如何将标题映射到 pandas 中的列? [英] How can I map the headers to columns in pandas?
本文介绍了如何将标题映射到 pandas 中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,如:
A B C
1 0 0
1 1 0
0 1 0
0 0 1
我想要:
A B C label
1 0 0 A
1 1 0 AB
0 1 0 B
0 0 1 C
我试图通过地图或申请来做,但我不知道.
I've tried to do by map or apply but I couldn't figured it out.
推荐答案
这是一种惯用而高效的解决方案
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
这篇关于如何将标题映射到 pandas 中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文