Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—每次A列等于0时,从B列返回值_Python_Pandas_Numpy_Dataframe - Fatal编程技术网

Python—每次A列等于0时,从B列返回值

Python—每次A列等于0时,从B列返回值,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,每次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).id

每次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的每个实例吗?

pandas
提供了许多方法来完成此任务。至于哪一个是最好的、最具python风格的(不管是什么意思)还是pandantic(另一个虚构的词),这完全是个人的决定

使用
查询

df.query('A==0').B



df.query('A==0')['B']
提供了许多方法来完成此任务。至于哪一个是最好的、最具python风格的(不管是什么意思)还是pandantic(另一个虚构的词),这完全是个人的决定

使用
查询

df.query('A==0').B


df.query('A==0')['B']
一种方法是

df[df['A']==0]['B']
一种方法是

df[df['A']==0]['B']
用于:

用于:


这是一个泛张力(pythonic)答案。这是也不应该被鼓励的,如果您试图修改它,这里返回的数据将发出警告。这是一个泛张力(pythonic)答案。这是也不应该被鼓励的,如果您试图修改,此处返回的数据将引发警告。此处的选项2不会引发复制警告设置,原因是?此处的选项2不会引发复制警告设置,原因是?此IMO是最类似熊猫的方法,因为这将返回数据视图,而不是副本,并且在修改此IMO时不会引发警告。此IMO是最常见的方法pandas喜欢此方法,因为它将返回数据视图而不是副本,并且在修改时不会发出警告