I have a dataframe that looks like
ID Cat1 Cat2 Cat3 Cat4
3432432 True False True False
1242323 False True False False
3423883 False False False True
How can I convert that to a dataframe that chooses the first column that is True?
ID Status
3432432 Cat1
1242323 Cat2
3423883 Cat4
You could take advantage of the fact that idxmax
will return the first True:
>>> df.set_index("ID").idxmax(axis=1).reset_index(name="Status")
ID Status
0 3432432 Cat1
1 1242323 Cat2
2 3423883 Cat4
which works because we have
>>> df.iloc[:,1:]
Cat1 Cat2 Cat3 Cat4
0 True False True False
1 False True False False
2 False False False True
>>> df.iloc[:,1:].idxmax(axis=1)
0 Cat1
1 Cat2
2 Cat4
dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With