查找每行具有最大值的列名

2023-12-22

我有一个像这样的数据框:

Communications and Search   Business    General Lifestyle
0   0.745763    0.050847    0.118644    0.084746
0   0.333333    0.000000    0.583333    0.083333
0   0.617021    0.042553    0.297872    0.042553
0   0.435897    0.000000    0.410256    0.153846
0   0.358974    0.076923    0.410256    0.153846

我想获取每行具有最大值的列名。期望的输出是这样的:

Communications and Search   Business    General Lifestyle  Max
0   0.745763    0.050847    0.118644    0.084746           Communications 
0   0.333333    0.000000    0.583333    0.083333           Business  
0   0.617021    0.042553    0.297872    0.042553           Communications 
0   0.435897    0.000000    0.410256    0.153846           Communications 
0   0.358974    0.076923    0.410256    0.153846           Business 

您可以使用idxmax http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.idxmax.html with axis=1查找每行中具有最大值的列:

>>> df.idxmax(axis=1)
0    Communications
1          Business
2    Communications
3    Communications
4          Business
dtype: object

要创建新列“Max”,请使用df['Max'] = df.idxmax(axis=1).

为了找到row每列中出现最大值的索引,使用df.idxmax()(或同等地df.idxmax(axis=0)).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查找每行具有最大值的列名 的相关文章

随机推荐