我希望每次A列等于0时都返回B列的值。我当前的代码:
for i in df:
checkfornulls = df.ix[(df['A'][i] == 0).idxmin(),'B']
if checkfornulls is None:
print "Column A Pass!"
else:
print checkfornulls
如果我只是在它上面运行它:
checkfornulls = df.ix[(df['A'][i] == 0).idxmin(),'B']
if checkfornulls is None:
print "Column A Pass!"
else:
print checkfornulls
然后它会找回它找到的第一个0。有没有人知道如何使for循环工作,以便它返回A等于0的每个实例?
答案 0 :(得分:3)
pandas
提供了许多方法来完成此任务。这是一个个人决定,哪个是最好的,最pythonic(无论那意味着什么),或pandantic(另一个组成单词)。
使用query
df.query('A == 0').B
或
df.query('A == 0')['B']
答案 1 :(得分:2)
这样做的一种方法是
df[df['A']==0]['B']
答案 2 :(得分:2)
df.loc[df['A']==0, 'B']