【发布时间】:2017-08-12 07:01:24
【问题描述】:
我正在大熊猫数据框中搜索大量单词,但我遇到了性能问题。有没有办法在 pandas 数据框中的列的字符串中进行二进制搜索?
现在我的代码是这样的:
names = pd.DataFrame(data=['one', 'two', 'three', 'four'], index=range(0, 4), columns=['Name'])
sentence = 'There are two trees in the street.'
for word in word_tokenize(sentence):
# Search for each word in all the names
new_names = names[names['Name'].str.startswith(word)]
# then do some operations on the names
但我需要为names[names['Name'].str.startswith(word)] 提供更好的性能,并且我认为我应该找到一种在“名称”列上进行二分搜索的方法。
【问题讨论】:
-
你到底尝试了什么?您需要提供更多细节。提供带有您尝试过的一些代码的示例 DataFrame 将大有帮助。
-
@TedPetrou 谢谢!我稍微改变了这个问题。
-
仍然没有足够的细节来提供答案。
iterrows下面发生了什么。您通常应该不惜一切代价避免使用iterrows。包含更多信息的示例数据框将大有帮助。 -
@TedPetrou 我在开头添加了一个示例数据。
iterrows并不重要。我可以使用其他方法进行下一步操作。主要问题是当它变得太大时在数据框中进行搜索。 -
@AmirAhmad,您可能需要查看this approach
标签: python string pandas search dataframe