Reputation: 4575
I have a dataframe like the one below. I would like to find all columns in the dataframe that contain a string ‘abc’ and return a list of those fields like the example below. If I was looking for the rows I would use isin, but I'm not sure how to get the columns. Any tips are greatly appreciated.
Example:
Print df
Field1 Field2 Field3
f_abc 23 dog
Df3 bb ju
Return
Field1
Upvotes: 1
Views: 2609
Reputation: 21
You can also make use of pandas pd.Series.isin
which returns a boolean series.
mydf = pd.DataFrame(data={'Field1':['f_abc', 21], 'Field2':[23, 'bb'], 'Field3':['dog', 'ju']}, columns=['Field1', 'Field2', 'Field3'])
print([i for i in mydf if mydf[i].isin(['f_abc']).any()])
(don't yet have the reputation to have added this as a comment)
Upvotes: 2
Reputation: 1109
You can accomplish this with list comprehension on df.columns
.
First let's create an example dataframe:
df = pd.DataFrame(columns=["A", "B", "C"], data=[['abc']*3]*3)
df['D'] = 'ab'
results in:
A B C D
0 abc abc abc ab
1 abc abc abc ab
2 abc abc abc ab
Now let's print only the columns that contain 'abc':
print([col for col in df.columns if df[col].str.contains("abc").any()])
['A', 'B', 'C']
Upvotes: 5