简体   繁体   中英

Conditional filter multiple columns python

I have 5 columns, each of them has 0's and 1's in each row. I need to filter all those with '1' at once.

I tried this but results in error:

df_2 = df_1[df_1.columns[0:5] == 1]
ValueError: Item wrong length 2 instead of 111249

I believe you need any if want filter at least one 1 per rows of filtered columns:

df_2 = df_1[(df_1.columns[0:5] == 1).any(axis=1)]

Or all if want filter all 1 per rows of filtered columns:

df_2 = df_1[(df_1.columns[0:5] == 1).all(axis=1)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM