简体   繁体   English

select行如何根据pandas中某列范围的特定条件

[英]How to select rows based on specific conditions of a range of columns in pandas

Suppose, I have the following dataframe:假设,我有以下 dataframe:

A B C D E F
1 1 1 0 0 0
0 0 0 0 0 0
1 1 0.9 1 0 0
0 0 0 0 -1.95 0
0 0 0 0 2.75 0
1 1 1 1 1 1

I want to select rows which have only zeros or ones based on the columns C, D, E and F .我想要 select 行,其中只有零或基于列C, D, E and F的行。 For this example, the expected output is对于此示例,预期的 output 是

 A B C D E F
 1 1 1 0 0 0
 0 0 0 0 0 0
 1 1 1 1 1 1

How can I do this with considering a range of columns in pandas?考虑 pandas 中的一系列列,我该如何做到这一点?

Thanks in advance.提前致谢。

df.loc[df[['C','D','E','F']].isin([1,0]).all(axis=1)]

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

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