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

Viewed 83

Suppose, I have the following 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. For this example, the expected output is

 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?

Thanks in advance.

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