You can filter each column separately - convert Animal
to index by DataFrame.set_index
and compare by DataFrame.eq
, then filter by columns names and index convert to list:
df1 = df.set_index('Animal').eq('Yes')
print (df1.index[df1['wings']].tolist())
['Bat']
print (df1.index[df1['claws']].tolist())
['Bat', 'Cat']
print (df1.index[df1['horns']].tolist())
[]
Or is possible create dictionary use dict comprehension:
df1 = df.set_index('Animal').eq('Yes')
d = {col: df1.index[df1[col]].tolist() for col in df1}
print (d)
{'wings': ['Bat'], 'claws': ['Bat', 'Cat'], 'horns': []}
Or if want separator add join
:
d1 = {col: ','.join(df1.index[df1[col]]) for col in df1}
print (d1)
{'wings': 'Bat', 'claws': 'Bat,Cat', 'horns': ''}
Or if want filter all columns to Series with index by Animal
and values by all Yes
values with separator ,
use DataFrame.dot
with transpose DataFrame
:
df1 = df.set_index('Animal').eq('Yes')
s = df1.T.dot(df1.index + ',').str.strip(',')
print (s)
wings Bat
claws Bat,Cat
horns
dtype: object
print (s['wings'])
Bat
print (s['claws'])
Bat,Cat
print (s['horns'])