列名称为:ID、1、2、3、4、5、6、7、8、9。
col 值为 0 或 1
我的数据框如下所示:
ID 1 2 3 4 5 6 7 8 9
1002 0 1 0 1 0 0 0 0 0
1003 0 0 0 0 0 0 0 0 0
1004 1 1 0 0 0 0 0 0 0
1005 0 0 0 0 1 0 0 0 0
1006 0 0 0 0 0 1 0 0 0
1007 1 0 1 0 0 0 0 0 0
1000 0 0 0 0 0 0 0 0 0
1009 0 0 1 0 0 0 1 0 0
我想要列名位于 ID 前面,其中一行中的值为 1。
我想要的数据框应该是这样的:
ID Col2
1002 2 // has 1 at Col(2) and Col(4)
1002 4
1004 1 // has 1 at col(1) and col(2)
1004 2
1005 5 // has 1 at col(5)
1006 6 // has 1 at col(6)
1007 1 // has 1 at col(1) and col(3)
1007 3
1009 3 // has 1 at col(3) and col(7)
1009 7
请帮助我,提前致谢
漂亮的一句台词:)
new_df = df.idxmax(axis=1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)