Pandas:找到每行的max(和second max)值的列名和值

时间:2015-02-19 15:05:28

标签: python pandas

我输入的东西看起来像DF1(下面要生成的代码),并希望在输出中看起来像DF2。

我们的想法是为每一行找到该行中具有最高值的列名,相应的值,以及该行中具有第二高值的列名,以及其对应的值。

使用pandas有简单的方法吗?

import pandas as pd
DF1 = pd.DataFrame({'User' : pd.Series(["Line1","Line2","Line3", "Line4"], index=['1', '2','3','4']), 'Var1' : pd.Series([9,12,3,21], index=['1', '2','3','4']),'Var2' : pd.Series([8,16,3,2], index=['1', '2','3','4']),'Var3' : pd.Series([7,5,6,9], index=['1', '2','3','4']),'Var4' : pd.Series([10,13,20,20], index=['1', '2','3','4']),'Var5' : pd.Series([8,2,13,1], index=['1', '2','3','4']),'Var6' : pd.Series([4,4,7,11], index=['1', '2','3','4']),'Var7' : pd.Series([15,13,4,7], index=['1', '2','3','4'])})
DF1

DF2 = pd.DataFrame({'User' : pd.Series(["Line1","Line2","Line3", "Line4"], index=['1', '2','3','4']), 'Max1Name' : pd.Series(["Var7","Var2","Var4","Var1"], index=['1', '2','3','4']),'Max1Value' : pd.Series([15,16,20,21], index=['1', '2','3','4']),'Max2Name' : pd.Series(["Var4","Var4","Var5","Var4"], index=['1', '2','3','4']),'Max2Value' : pd.Series([10,13,13,20], index=['1', '2','3','4'])})
DF2

1 个答案:

答案 0 :(得分:4)

不确定这是否是最简单的方法,但您可以这样做:

def top(x):
    x.set_index('User', inplace=True)
    df = pd.DataFrame({'Max1Name':[],'Max2Name':[],'Max1Value':[],'Max2Value':[]})
    df.index.name='User'
    df.loc[x.index.values[0],['Max1Name', 'Max2Name']] = x.sum().nlargest(2).index.tolist()
    df.loc[x.index.values[0],['Max1Value', 'Max2Value']] = x.sum().nlargest(2).values
    return df

DF1.groupby('User').apply(top).reset_index(level=1, drop=True).reset_index()

产生所需的输出:

    User Max1Name  Max1Value Max2Name  Max2Value
0  Line1     Var7         15     Var4         10
1  Line2     Var2         16     Var4         13
2  Line3     Var4         20     Var5         13
3  Line4     Var1         21     Var4         20

更简单的方法是:

DF1.groupby('User').apply(lambda x: x.set_index('User').sum().nlargest(2))

用户可以获得前2名:

    User       
Line1  Var7    15
       Var4    10
Line2  Var2    16
       Var4    13
Line3  Var4    20
       Var5    13
Line4  Var1    21
       Var4    20
dtype: int64