Find the maximum values of a column in multiindex dataframe and return all its values

时间:2018-04-18 18:09:20

标签: python python-3.x pandas multi-index

Reproducible code for the dataset:

df = {'player' : ['a','a','a','a','a','a','a','a','a','b','b','b','b','b','b','b','b','b','c','c','c','c','c','c','c','c','c'],
      'week' : ['1','1','1','2','2','2','3','3','3','1','1','1','2','2','2','3','3','3','1','1','1','2','2','2','3','3','3'],
      'category': ['RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH','RES','VIT','MATCH'],
      'energy' : [75,54,87,65,24,82,65,42,35,25,45,87,98,54,82,75,54,87,65,24,82,65,42,35,25,45,98] }

df = pd.DataFrame(data= df)
df = df[['player', 'week', 'category','energy']]

Actual Dataset

I need to find "For each player, Find the week where his energy was maximum and display all the categories, energy values for that week"

So what I did was:

1.Set Player and Week as Index

2.Iterate over the index to find the max value of energy and return its value

df = df.set_index(['player', 'week'])

for index, row in df1.iterrows():
    group = df1.ix[df1['energy'].idxmax()]

Output Obtained:

                category energy
  player   week     
    b        2    RES      98
             2    VIT      54
             2   MATCH     82

This obtained output is for the maximum energy in the entire dataset, I would want the maximum for each player with the all other categories and its energy for that week.

Expected Output:

Expected Output

I have tried using groupby method as suggested in the comments,

df.groupby(['player','week'])['energy'].max().groupby(level=['player','week'])

The output obtained is:

                energy  category
 player week        
   a     1        87    VIT
         2        82    VIT
         3        65    VIT
   b     1        87    VIT
         2        98    VIT
         3        87    VIT
   c     1        82    VIT
         2        65    VIT
         3        98    VIT

3 个答案:

答案 0 :(得分:4)

找出每个玩家的最大能量周,然后为玩家选择那个星期,并在所有玩家之间连接结果。

max_energy_idx = df.groupby('player')['energy'].idxmax()  # 2, 12, 26
max_energy_weeks = df['week'].iloc[max_energy_idx]  # '1', '2', '3'
players = sorted(df['player'].unique())  # 'a', 'b', 'c'

result = pd.concat(
    [df.loc[(df['player'] == player) & (df['week'] == max_enery_week), :] 
     for player, max_enery_week in zip(players, max_energy_weeks)]
)
>>> result
   player week category  energy
0       a    1      RES      75
1       a    1      VIT      54
2       a    1    MATCH      87
12      b    2      RES      98
13      b    2      VIT      54
14      b    2    MATCH      82
24      c    3      RES      25
25      c    3      VIT      45
26      c    3    MATCH      98

如果需要,您可以在结果上设置索引:

result = result.set_index(['player', 'week'])

答案 1 :(得分:4)

df带有原始索引(即在设置多索引之前),您可以通过使用.merge执行内部联接来获得一行结果:

df.merge(df.loc[df.groupby('player').energy.idxmax(), ['player', 'week']])

#   player week category  energy
# 0      a    1      RES      75
# 1      a    1      VIT      54
# 2      a    1    MATCH      87
# 3      b    2      RES      98
# 4      b    2      VIT      54
# 5      b    2    MATCH      82
# 6      c    3      RES      25
# 7      c    3      VIT      45
# 8      c    3    MATCH      98

答案 2 :(得分:3)

没有concat的另一种解决方案:

idx = df.groupby('player')['energy'].idxmax() 

coord = df.iloc[idx]

coord
player  week    category    energy
2   a   1   MATCH   87
12  b   2   RES 98
26  c   3   MATCH   98


df.set_index(['player', 'week']).loc[(df.iloc[idx].set_index(['player', 'week']).index)]
category    energy
player  week        
a   1   RES 75
    1   VIT 54
    1   MATCH   87
b   2   RES 98
    2   VIT 54
    2   MATCH   82
c   3   RES 25
    3   VIT 45
    3   MATCH   98