How can I map the headers to columns in pandas?

前端 未结 5 1571
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  -上瘾入骨i
    2020-12-16 01:47

    Using dot

    df.assign(label=df.dot(df.columns))
    
       A  B  C label
    0  1  0  0     A
    1  1  1  0    AB
    2  0  1  0     B
    3  0  0  1     C
    

    Same thing using underlying numpy arrays

    df.assign(label=df.values.dot(df.columns.values))
    
       A  B  C label
    0  1  0  0     A
    1  1  1  0    AB
    2  0  1  0     B
    3  0  0  1     C
    

提交回复
热议问题