Get columns which are binary from a dataframe

In [1]:
import pandas as pd

In [2]:
df = pd.DataFrame({'col_1': [1, 0, 1, None], 
                   'col_2': [1.2, 3.1, 4.4, 5.5], 
                   'col_3': [1, 2, 3, 4], 
                   'col_4': ['a', 'b', 'c', 'd']})

In [3]:
df


Out[3]:
col_1 col_2 col_3 col_4
0 1.0 1.2 1 a
1 0.0 3.1 2 b
2 1.0 4.4 3 c
3 NaN 5.5 4 d

In [4]:
bool_cols = [col for col in df if len(df[[col]].dropna()[col].unique()) == 2]

In [5]:
bool_cols


Out[5]:
['col_1']

In [6]:
df[bool_cols]


Out[6]:
col_1
0 1.0
1 0.0
2 1.0
3 NaN

In [ ]: