lalala53__
lalala53__

Reputation: 25

pandas dataframe select all columns that cointain specific string value

I'm looking to select all columns that contain a string value in any of the rows and add that column to a list for manipulation later on.

Could you please help find the way? extract from my df:

d = {'1':['Q4 2018', 100, 111, 222], 
     '2':[2018, 333, 444, 555],
     '3':['Q1 2019', 55, 789, 70]}
df = pd.DataFrame(d)

I'd like to see a list of columns that contain value of 'Q1', 'Q2', 'Q3', 'Q4' anywhere in the column. In this case columns 1 and 3.

Upvotes: 0

Views: 364

Answers (1)

BENY
BENY

Reputation: 323226

Using applymap with any

df.applymap(lambda x : 'Q' in str(x)).any()
Out[268]: 
1     True
2    False
3     True
dtype: bool

Upvotes: 1

Related Questions