I have a Dataframe like this,
col1 col2 col3 col4 col5 col6
abc bc eg egg 123 NA
frog dog fox cat ac aa
12 7 87 ch 25 1
bc 79 09 123 NA 89
...
...
I want to select all columns with contains a specific string to get a subset for the dataframe.
For example, I want all rows contains 'bc'
I know how to select it from one column,
df.loc[df.col1.str.contains('bc', na=False)]
But how can I get the data from all columns at once? Because my original columns are more than 200+.
I tried to use,
for c, dtype in zip(df.columns, df.dtypes):
... if dtype == np.object:
... df = df.loc[df[c].str.contains("bc",na = False)]
But it only returns all column names.
The final results should be like,
col1 col2 col3 col4 col5 col 6
abc bc efg egg 123 NA
bc 79 09 123 NA 89
...
...
All I want is a subset of all rows of the original Dataframe contains 'bc'.