简体   繁体   English

测试 pandas DataFrame 的任何列是否满足条件

[英]Test if any column of a pandas DataFrame satisfies a condition

I got a DataFrame with lots of columns.我得到了一个包含很多列的 DataFrame。 Now I have a condition that tests some of those columns if any of that column-set is different to zero.现在我有一个条件来测试其中一些列,如果任何列集不为零。

Is there any more elegant way to apply that condition to a subset of columns?有没有更优雅的方法可以将该条件应用于列的子集? My current code is:我目前的代码是:

df['indicator'] = (
    (df['col_1'] != 0) | 
    (df['col_2'] != 0) | 
    (df['col_3'] != 0) | 
    (df['col_4'] != 0) | 
    (df['col_5'] != 0)
)

I was looking for something like this pseudo code:我正在寻找类似这个伪代码的东西:

columns = ['col_1', 'col_1', 'col_2', 'col_3', 'col_4', 'col_5']
df['indicator'] = df.any(columns, lambda value: value != 0)

ne is the method form of != . ne!=的方法形式。 I use that so that pipelining any looks nicer.我使用,使流水线any看起来更好。 I use any(axis=1) to find if any are true in a row.我使用any(axis=1)来查找一行中是否有任何为真。

df['indicator'] = df[columns].ne(0).any(axis=1)

In this particular case you could also check whether the sum of corresponding columns !=0 :在这种特殊情况下,您还可以检查相应列的总和!=0

df['indicator'] = df[columns].prod(axis=1).ne(0)

PS @piRSquared's solution is much more generic... PS @piRSquared 的解决方案更通用...

也许使用min

df['indicator']=(df[columns]!=0).min(axis=1).astype(bool)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何满足特殊条件的 output 和 Pandas DataFrame? - How to output a Pandas DataFrame that satisfies a special condition? 如果条件满足,如何应用熊猫并将条件满足时写入新列? - How to apply if condition with pandas and write it to new column if the condition satisfies? 尝试遍历熊猫数据框的行并在满足条件时编辑行 - Trying to iterate through rows of pandas dataframe and edit row if it satisfies a condition 在熊猫数据框中 - 返回满足条件的累积总和的最后一个值 - In pandas dataframe - returning last value of cumulative sum that satisfies condition DataFrame 中的列差异满足条件的出现次数 - Num of occurances where Column difference in DataFrame satisfies a condition 更改满足 dataframe 中条件的列中的一些特定值 - Change some specific values in a column that satisfies the condition in a dataframe Pandas:如果特定列满足特定条件,则选择行 - Pandas: select rows if a specific column satisfies a certain condition Pandas 将满足大于和小于条件的列的值倍数 - Pandas to multiple the value of a column that satisfies greater and less than condition 当pandas中的列满足某个条件时如何拉第一个实例? - How to pull the first instance when a column satisfies a certain condition in pandas? 适用于 pandas dataframe 列的条件 - Apply condition for pandas dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM