Test if any column of a pandas DataFrame satisfies

2020-07-10 08:36发布

I got a DataFrame with lots of columns. 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)

3条回答
该账号已被封号
2楼-- · 2020-07-10 08:48

Maybe using min

df['indicator']=(df[columns]!=0).min(axis=1).astype(bool)
查看更多
再贱就再见
3楼-- · 2020-07-10 08:58

In this particular case you could also check whether the sum of corresponding columns !=0:

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

PS @piRSquared's solution is much more generic...

查看更多
虎瘦雄心在
4楼-- · 2020-07-10 09:04

ne is the method form of !=. I use that so that pipelining any looks nicer. I use any(axis=1) to find if any are true in a row.

df['indicator'] = df[columns].ne(0).any(axis=1)
查看更多
登录 后发表回答