DEV Community

Nilotpal Choudhury
Nilotpal Choudhury

Posted on

Answer: How to check if column is binary? (Pandas)

If want filter columns names with 0 or 1 values:

c = df.columns[df.isin([0,1]).all()] print (c) Index(['col', 'col2'], dtype='object') 

If need filter columns:

df1 = df.loc[:, df.isin([0,1]).all()] print (df1) col col2 0 0 1 1 0 0 2 0 0 3 0 0 4 0 1 

Top comments (0)