[英]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.