找到每行具有最大值的列名称

mar*_*ain 97 python max dataframe pandas

我有一个像这样的DataFrame:

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
Run Code Online (Sandbox Code Playgroud)

在这里,我想询问如何获取每行具有最大值的列名,所需的输出如下:

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 
Run Code Online (Sandbox Code Playgroud)

Ale*_*ley 134

您可以使用idxmaxwith axis=1来查找每行中值最大的列:

>>> df.idxmax(axis=1)
0    Communications
1          Business
2    Communications
3    Communications
4          Business
dtype: object
Run Code Online (Sandbox Code Playgroud)

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

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

  • 只有一个问题:如果各列的值相同,则 idmax 选择第一列作为默认值......不理想...... (3认同)
  • @SushantKulkarni 你是如何获得前 3 名的概率而不是前 1 名的? (2认同)

use*_*097 11

如果要生成包含具有最大值但仅考虑列的子集的列的名称的列,则使用@ ajcr的答案的变体:

df['Max'] = df[['Communications','Business']].idxmax(axis=1)
Run Code Online (Sandbox Code Playgroud)

  • 如果你想排除除子集之外的所有列`df ['Max'] = df [df.columns.difference(['Foo','Bar'])].idxmax(axis = 1)` (3认同)

Zer*_*ero 6

您可以apply在数据框上并argmax()通过获取每一行axis=1

In [144]: df.apply(lambda x: x.argmax(), axis=1)
Out[144]:
0    Communications
1          Business
2    Communications
3    Communications
4          Business
dtype: object
Run Code Online (Sandbox Code Playgroud)

这里有一个基准来比较慢apply的方法是idxmax()len(df) ~ 20K

In [146]: %timeit df.apply(lambda x: x.argmax(), axis=1)
1 loops, best of 3: 479 ms per loop

In [147]: %timeit df.idxmax(axis=1)
10 loops, best of 3: 47.3 ms per loop
Run Code Online (Sandbox Code Playgroud)


cot*_*ail 6

另一种解决方案是标记每行最大值的位置并获取相应的列名称。特别是,如果多个列包含某些行的最大值并且您希望返回每行具有最大值的所有列名称,则此解决方案效果很好:1

情况1

代码:

# look for the max values in each row
mxs = df.eq(df.max(axis=1), axis=0)
# join the column names of the max values of each row into a single string
df['Max'] = mxs.dot(mxs.columns + ', ').str.rstrip(', ')
Run Code Online (Sandbox Code Playgroud)

略有不同:如果您想在多列包含最大值时 随机选择一列:案例2

代码:

mxs = df.eq(df.max(axis=1), axis=0)
df['Max'] = mxs.where(mxs).stack().groupby(level=0).sample(n=1).index.get_level_values(1)
Run Code Online (Sandbox Code Playgroud)

您还可以通过选择列来对特定列执行此操作:

# for column names of max value of each row
cols = ['Communications', 'Search', 'Business']
mxs = df[cols].eq(df[cols].max(axis=1), axis=0)
df['max among cols'] = mxs.dot(mxs.columns + ', ').str.rstrip(', ')
Run Code Online (Sandbox Code Playgroud)

1:idxmax(1)如果多个列的最大值相同,则仅返回具有最大值的第一个列名称,根据用例,这可能并不理想。该解决方案概括了idxmax(1);特别是,如果每行中的最大值都是唯一的,则它与idxmax(1)解决方案匹配。