Use DataFrame.isin
for check all columns and DataFrame.any
for check at least one True
per row:
m = df.isin(my_word).any()
print (m)
0 False
1 True
2 False
dtype: bool
And then get columns names by filtering:
cols = m.index[m].tolist()
print(cols)
[1]
Data:
print (df)
0 1 2
0 NaN good employee
1 Not available best employer
2 not required well manager
3 not eligible super reportee
Detail:
print (df.isin(my_word))
0 1 2
0 False False False
1 False False False
2 False True False
3 False False False
print (df.isin(my_word).any())
0 False
1 True
2 False
dtype: bool
EDIT After converting get nested list
s, so flattening is necessary:
my_word=["well","manager"]
m = df.isin(my_word).any()
print (m)
0 False
1 True
2 True
dtype: bool
nested = df.loc[:,m].values.tolist()
flat_list = [item for sublist in nested for item in sublist]
print (flat_list)
['good', 'employee', 'best', 'employer', 'well', 'manager', 'super', 'reportee']