How can I map the headers to columns in pandas?

前端 未结 5 1575
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 01:15

I have a dataframe like :

A    B    C 
1    0    0
1    1    0
0    1    0
0    0    1

I want to have :

 A    B    C  lab         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-16 02:07

    Here is an idiomatic and performant solution

    df['label'] = np.where(df, df.columns, '').sum(axis=1)
    
       A  B  C label
    0  1  0  0     A
    1  1  1  0    AB
    2  0  1  0     B
    3  0  0  1     C
    

提交回复
热议问题