I am trying to obtain a list of columns in a DataFrame if any value in a column contains a string. For example in the below dataframe I would like a list of columns that have the % in the string. I am able to accomplish this using a for loop and the series.str.contains method but doens't seem optimal especially with a larger dataset. Is there a more efficient way to do this?
import pandas as pd
df = pd.DataFrame({'A': {0: '2019-06-01', 1: '2019-06-01', 2: '2019-06-01'},
'B': {0: '10', 1: '20', 2: '30'},
'C': {0: '10', 1: '20%', 2: '30%'},
'D': {0: '10%', 1: '20%', 2: '30'},
})
DataFrame
A B C D
0 2019-06-01 10 10 10%
1 2019-06-01 20 20% 20%
2 2019-06-01 30 30% 30
Current Method
col_list = []
for col in df.columns:
if (True in list(df[col].str.contains('%'))) is True:
col_list.append(col)
Output
['C', 'D']
df.dtypes?