【问题标题】:Find the maximum values of a column in multiindex dataframe and return all its values在多索引数据框中查找列的最大值并返回其所有值
【发布时间】:2018-09-29 02:05:16
【问题描述】:

数据集的可重现代码:

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']]

我需要找到“对于每个玩家,找到他能量最大的那一周,并显示所有类别,该周的能量值”

所以我做的是:

1.设置播放器和周为索引

2.遍历索引以找到能量的最大值并返回其 价值

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

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

获得的输出:

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

这个获得的输出是整个数据集中的最大能量,我希望每个玩家的最大值与所有其他类别及其在那一周的能量。

预期输出:

我已尝试使用 cmets 中建议的 groupby 方法,

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

得到的输出是:

                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

【问题讨论】:

  • 你是否尝试过按周分组并消耗最大能量
  • 我很累 df.groupby(by=['player','week'])['energy','category'].max(),但它没有给我预期的输出。
  • @cmaher 我已经尝试过该解决方案,但它没有给我预期的输出。它返回我能够获得的整个数据集的最大值。但我试图找到每个玩家的最大值,并找到最大值出现的那一周,并返回该周的所有数据。

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


【解决方案1】:

找出每个玩家的最大能量周,然后为该玩家选择该周,并将所有玩家的结果连接起来。

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'])

【讨论】:

  • 完美运行@Alexander。正是我正在寻找的。我从没想过这样做。也非常感谢您的解释。非常感谢:)
【解决方案2】:

没有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

【讨论】:

  • 这个解决方案也很完美,也更简单。谢谢@Boud
【解决方案3】:

将您的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

【讨论】:

  • 这个解决方案更干净。
猜你喜欢
  • 1970-01-01
  • 2019-12-14
  • 2016-07-19
  • 2019-04-13
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
相关资源
最近更新 更多