Reputation: 941
I have this df:
A B C D
a d a z
b c z c
My goal is to pull out the column names where df == 'z' so I used:
df['ColumnNames'] = df.where(df=='z' ).stack().reset_index().groupby('level_0')['level_1'].apply('+ '.join)
It used to work fine; however today I got an error saying: "TypeError: Could not compare ['z'] with block values"
Does anyone know why this happens? Many thanks!
Upvotes: 0
Views: 85
Reputation: 323346
Your code working fine on my side, check whether contain space in your df , also we short it with
df[df=='z'].stack()
Out[216]:
0 D z
1 C z
dtype: object
df[df=='z'].stack().index.get_level_values(1)
Out[218]: Index(['D', 'C'], dtype='object')
From the Comment above by PiR
My guess is that you have a version of pandas that is borking when comparing int to str. Try df.astype(object) == 'z'
Upvotes: 2