简体   繁体   English

条件过滤多列 python

[英]Conditional filter multiple columns python

I have 5 columns, each of them has 0's and 1's in each row.我有 5 列,每列的每一行都有 0 和 1。 I need to filter all those with '1' at once.我需要一次过滤所有带有“1”的人。

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:我相信你需要any如果想要过滤每行过滤列至少一个1

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:或者如果想要过滤all过滤列的所有1行:

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

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

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