Option 1
Using pd.DataFrame.dropna with pd.DataFrame.mask
The concept is that I replace 'a' with np.nan and then conveniently use dropna.
This drops the column even it has one a.
df.mask(df.astype(str).eq('a')).dropna(1) Col1 Col2 1 b g 2 d z 3 g x 4 h p 5 b c
This requires that all elements of the column be a
df.mask(df.astype(str).eq('a')).dropna(1, how='all') Col1 Col2 1 b g 2 d z 3 g x 4 h p 5 b c
Option 2
Creative way using np.where to find the unique column positions that have 'a'
This is cool because np.where will return a tuple of arrays that give the positions of all True values in an array. The second array of the tuple will be all the column positions. I grab a unique set of those and find the other column names.
df[df.columns.difference( df.columns[np.unique(np.where(df.astype(str).eq('a'))[1] )])] Col1 Col2 1 b g 2 d z 3 g x 4 h p 5 b c
Or similarly with pd.DataFrame.drop
df.drop(df.columns[np.unique(np.where(df.astype(str).eq('a'))[1])], 1) Col1 Col2 1 b g 2 d z 3 g x 4 h p 5 b c
Option 3
Probably bad way of doing it.
df.loc[:, ~df.astype(str).sum().str.contains('a')] Col1 Col2 1 b g 2 d z 3 g x 4 h p 5 b c