I have this df:
JavaScript
x
8
1
data = {'book': [True, False, False, False, False],
2
'apple': [False, False, True, False, False],
3
'cat': [False, False, False, False, True],
4
'pigeon': [False, True, False, False, False],
5
'shirt': [False, False, False, True, False]}
6
7
df = pd.DataFrame(data)
8
Then I want create a new column, df['category']
that takes in as value, the column’s name whose value is true.
So that df['category']
for each TRUE value column as follows:
JavaScript
1
2
1
book - stationery, apple - fruit, cat - animal, pigeon - bird, shirt - clothes
2
NO 2 columns have TRUE value in a row.
Expected output:
JavaScript
1
8
1
>>> df
2
book apple cat pigeon shirt category
3
0 True False False False False stationery
4
1 False False False True False bird
5
2 False True False False False fruit
6
3 False False False False True clothes
7
4 False False True False False animal
8
Advertisement
Answer
Simple..use idxmax
along axis=1
to get the name of column having True
value, then map
the name to the corresponding category
JavaScript
1
5
1
d = {'book': 'stationery', 'pigeon': 'bird',
2
'apple': 'fruit', 'shirt': 'clothes', 'cat': 'animal'}
3
4
df['category'] = df.idxmax(1).map(d)
5
JavaScript
1
7
1
book apple cat pigeon shirt category
2
0 True False False False False stationery
3
1 False False False True False bird
4
2 False True False False False fruit
5
3 False False False False True clothes
6
4 False False True False False animal
7