如何在 pandas 中使用基于DataFrame布尔值的条件语句 [英] How to use a conditional statement based on DataFrame boolean value in pandas

查看:83
本文介绍了如何在 pandas 中使用基于DataFrame布尔值的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我知道如何检查数据框中多列中的特定值.但是,我似乎无法弄清楚如何基于布尔响应来执行if语句.

Now I know how to check the dataframe for specific values across multiple columns. However, I cant seem to work out how to carry out an if statement based on a boolean response.

例如:

使用os.walk遍历目录,并将特定文件读入数据框.

Walk directories using os.walk and read in a specific file into a dataframe.

for root, dirs, files in os.walk(main):
        filters = '*specificfile.csv'
        for filename in fnmatch.filter(files, filters):
        df = pd.read_csv(os.path.join(root, filename),error_bad_lines=False)

现在检查跨多个列的数据框.第一个值是列名(column1),下一个值是我在该列(香蕉)中寻找的特定值.然后,我正在检查另一列(column2)的特定值(绿色).如果这两个都是正确的,我想执行一项特定的任务.但是,如果它是错误的,我想做其他事情.

Now checking that dataframe across multiple columns. The first value being the column name (column1), the next value is the specific value I am looking for in that column(banana). I am then checking another column (column2) for a specific value (green). If both of these are true I want to carry out a specific task. However if it is false I want to do something else.

类似:

if (df['column1']=='banana') & (df['colour']=='green'):
    do something
else: 
    do something

推荐答案

如果要检查DataFrame的任何行是否满足您的条件,可以使用

If you want to check if any row of the DataFrame meets your conditions you can use .any() along with your condition . Example -

if ((df['column1']=='banana') & (df['colour']=='green')).any():

示例-

In [16]: df
Out[16]:
   A  B
0  1  2
1  3  4
2  5  6

In [17]: ((df['A']==1) & (df['B'] == 2)).any()
Out[17]: True

这是因为您的条件-((df['column1']=='banana') & (df['colour']=='green'))-返回一系列True/False值.

This is because your condition - ((df['column1']=='banana') & (df['colour']=='green')) - returns a Series of True/False values.

这是因为在熊猫中,当将一个系列与一个标量值进行比较时,它将返回将该系列的每一行与该标量值进行比较的结果,并且结果是一系列True/False值,指示具有标量值的那一行.示例-

This is because in pandas when you compare a series against a scalar value, it returns the result of comparing each row of that series against the scalar value and the result is a series of True/False values indicating the result of comparison of that row with the scalar value. Example -

In [19]: (df['A']==1)
Out[19]:
0     True
1    False
2    False
Name: A, dtype: bool

In [20]: (df['B'] == 2)
Out[20]:
0     True
1    False
2    False
Name: B, dtype: bool

&对两个系列按行进行and.示例-

And the & does row-wise and for the two series. Example -

In [18]: ((df['A']==1) & (df['B'] == 2))
Out[18]:
0     True
1    False
2    False
dtype: bool

现在要检查该系列中的任何值是否为True,可以使用

Now to check if any of the values from this series is True, you can use .any() , to check if all the values in the series are True, you can use .all() .

这篇关于如何在 pandas 中使用基于DataFrame布尔值的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆