我正在熊猫数据框中搜索匹配项。我发现结果不一致或者我使用了错误的代码类型。我使用的是内部命令,但并不总是一致的。如果我比较值(参见示例),它是有效的。指挥部有问题吗?你知道吗
代码示例:
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
因为要将数据帧的部分转换为字符串,所以遇到了问题。如果您查看使用
str(report[0:len(report)])
时打印出来的内容,您将看到它不仅返回该数据帧的值,还返回一个可读的描述。Pandas缩短了这个输出,所以当你在终端上打印它时,它实际上并不长。你知道吗所以,它不是
in
语句中的bug。如果需要这样做,请使用问题中的第二段代码。你知道吗作为参考,
str(report[0:len(report)])
的输出如下所示:相关问题 更多 >
编程相关推荐