Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Compress Column Names to Cell Values where True

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
like image 931
John Avatar asked Feb 07 '23 22:02

John


1 Answers

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
like image 63
DSM Avatar answered Feb 11 '23 16:02

DSM