【发布时间】:2016-02-28 22:21:48
【问题描述】:
我正在尝试在 Panda 数据框中搜索匹配项。我发现结果不一致,或者我使用了错误类型的代码。我使用的 in 命令并不总是一致的。如果我比较值(参见示例),它会起作用。 in 命令有问题吗?
代码示例:
import pandas as pd
report = pd.DataFrame(columns = (['col1','col2']))
report
i = 0
while i < 100:
a = str(i)
addthis = pd.Series({'col1':a,'col2':'AG100'})
report = report.append(addthis,ignore_index=True)
i = i + 1
###this will find a match but not 100 of the time%
i = 0
while i < len(report):
if str(i) in str(report[0:len(report)]):
print('found match on ',i)
else:
print('No match found on ',i)
i = i + 1
###this will find a match 100of the time%
i = 0
while i < len(report):
if str(i) == report.ix[i,0]:
print('found match on ',i)
else:
print('No match found on ',i)
i = i + 1
【问题讨论】:
-
你应该尝试使用更多的pythonic方式来循环遍历数据框,例如
for row in report.iterrows(),而不是跟踪计数器