查找每行具有最大值的列名 [英] Find the column name which has the maximum value for each row

查看:44
本文介绍了查找每行具有最大值的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 DataFrame:

在 [7] 中:帧头()出[7]:通信和搜索业务 一般生活方式0 0.745763 0.050847 0.118644 0.0847460 0.333333 0.000000 0.583333 0.0833330 0.617021 0.042553 0.297872 0.0425530 0.435897 0.000000 0.410256 0.1538460 0.358974 0.076923 0.410256 0.153846

在这里,我想问一下如何获得每行都有最大值的列名,所需的输出是这样的:

在 [7] 中:帧头()出[7]:通信和搜索业务 一般生活方式 Max0 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 业务

解决方案

您可以使用 idxmax with axis=1 找到每一行中具有最大值的列:

<预><代码>>>>df.idxmax(轴=1)0 通讯1 业务2 通讯3 通讯4 业务数据类型:对象

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

要查找每列中出现最大值的索引,请使用df.idxmax()(或等效的df.idxmax(axis=0)).

I have a DataFrame like this one:

In [7]:
frame.head()
Out[7]:
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

In here, I want to ask how to get column name which has maximum value for each row, the desired output is like this:

In [7]:
    frame.head()
    Out[7]:
    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 

解决方案

You can use idxmax with axis=1 to find the column with the greatest value on each row:

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

To create the new column 'Max', use df['Max'] = df.idxmax(axis=1).

To find the row index at which the maximum value occurs in each column, use df.idxmax() (or equivalently df.idxmax(axis=0)).

这篇关于查找每行具有最大值的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆