这是我的代码,它搜索数据框的一列并在满足要求时返回一个值:
import pandas as pd
df=pd.read_csv("cl.csv")
count=0
print(len(df.sl_no))
print(len(df.sn_compare))
for i in range(len(df.sn_compare)):
if df.sn_compare[i] in df.sl_no:
print(df.margin[i])
count=count+1
print(count)
输出:
8258
8258
0.036621541
.
.
.
0.036621541
0.043934426
0.063724333
3770
为什么循环在达到 3770 后停止,理想情况下我希望它搜索所有 8259 行?
考虑以下示例(虽然我没有添加边距列)。但是对于for
循环,它会检查列值是否包含在其他列值中,因此添加.values
(我不知道是否会出现相同的情况,但在这种情况下,如果我不这样做,它将返回 0)。
import pandas as pd
df = pd.DataFrame({'sl_no':[1,2,3], 'sn_compare':[4,5,3]})
print(df)
的输出df
:
sl_no sn_compare
0 1 4
1 2 5
2 3 3
现在,
print(len(df.sl_no))
print(len(df.sn_compare))
count=0
for i in range(len(df.sn_compare)):
if df.sn_compare[i] in df.sl_no.values:
#print(df.margin[i])
count=count+1
print('Count is: ', count)
输出:
3
3
Count is: 1
count 的输出将是1
因为只3
匹配所以,count
变为 1。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句