How do I drop every column that only contains values from a list pandas

33 views Asked by At

I want to be able to drop all columns that only have values from the list that I pass in.

list_of_drops = ['A','B']
d ={'C1':['A','A','A','B','B','B'],
    'C2':['A','C','A','A','A','A'],
    'C3':['A','B','B','A','B','B'],
    'C4':['A','A','A','A','A','A'],
    'C5':['A','A','B','AC','A','B'],
    'C6':['A','A','AD','A','B','A']}

df = pd.DataFrame (d, columns = ['C1','C2','C3','C4','C5','C6'])

In this example, I want to create a dataframe that only has C1, C3, and C4.

1

There are 1 answers

0
Jonathan Hay On BEST ANSWER

To get rid of columns with just one value:

df= df.loc[:, (df != 'A').any(axis=0)]

To get rid of columns with only values from a list:

df= df.loc[:, (~df.isin(list_of_drops)).any(axis=0)]