Select columns if any of their rows contain a certain string
stack
with any
df.columns[df.stack().str.contains('%').any(level=1)]
Index(['C', 'D'], dtype='object')
comprehension
[c for c in df if df[c].str.contains('%').any()]
['C', 'D']
filter
[*filter(lambda c: df[c].str.contains('%').any(), df)]
['C', 'D']
Numpy's find
from numpy.core.defchararray import find
df.columns[(find(df.to_numpy().astype(str), '%') >= 0).any(0)]
Index(['C', 'D'], dtype='object')
First use DataFrame.select_dtypes
for filter only object columns, obviously string columns.
Then use DataFrame.applymap
for elementwise check values with DataFrame.any
for return True if at least one per column, so possible filter columns:
c = df.columns[df.select_dtypes(object).applymap(lambda x: '%' in str(x)).any()].tolist()
print (c)
['C', 'D']
Or use Series.str.contains
per columns, na
parameter should be omit if all strings columns:
f = lambda x: x.str.contains('%', na=False)
c = df.columns[df.select_dtypes(object).apply(f).any()].tolist()
print (c)
['C', 'D']
Try this:
df.columns[df.apply(lambda x: x.str.contains("\%")).any()]