【问题标题】:How to get the max out of a group by on two columns and sum on third in a pandas dataframe?如何在熊猫数据框中通过两列从组中获取最大值并在第三列中求和?
【发布时间】:2018-09-04 09:18:51
【问题描述】:

所以我在一个看起来像这样的熊猫数据框上使用了 group by

df.groupby(['year','month'])['AMT'].agg('sum')

我得到了这样的东西

year  month
2003  1            114.00
      2           9195.00
      3            300.00
      5            200.00
      6            450.00
      7             68.00
      8            750.00
      9           3521.00
      10           250.00
      11           799.00
      12          1000.00
2004  1           8551.00
      2           9998.00
      3          17334.00
      4           2525.00
      5          16014.00
      6           9132.00
      7          10623.00
      8           7538.00
      9           3650.00
      10          7733.00
      11         10128.00
      12          4741.00
2005  1           6965.00
      2           3208.00
      3           8630.00
      4           7776.00
      5          11950.00
      6          11717.00
      7           1510.00
              ...    
2015  7        1431441.00
      8         966974.00
      9        1121650.00
      10       1200104.00
      11       1312191.90
      12        482535.00
2016  1        1337343.00
      2        1465068.00
      3        1170113.00
      4        1121691.00
      5        1302936.00
      6        1518047.00
      7        1251844.00
      8         825215.00
      9        1491626.00
      10       1243877.00
      11       1632252.00
      12        750995.50
2017  1         905974.00
      2        1330182.00
      3        1382628.52
      4        1146789.00
      5        1201425.00
      6        1278701.00
      7        1172596.00
      8        1517116.50
      9        1108609.00
      10       1360841.00
      11       1340386.00
      12        860686.00

我想要的是从第三个求和列中选择最大值,以便最终数据框只有每年的最大值,例如:

year  month
2003      2           9195.00
2004      3          17334.00
2005      5          11950.00

...等等

我必须通过聚合向我的组添加什么才能做到这一点?

【问题讨论】:

    标签: python pandas dataframe pandas-groupby


    【解决方案1】:

    我认为需要DataFrameGroupBy.idxmax:

    s = df.groupby(['year','month'])['AMT'].sum()
    out = s.loc[s.groupby(level=0).idxmax()]
    #working in newer pandas versions
    #out = df.loc[df.groupby('Year').idxmax()]
    print (out)
    Year  month
    2003  2         9195.0
    2004  3        17334.0
    2005  5        11950.0
    Name: AMT, dtype: float64
    

    如果可能,每年有多个最大值:

    out = s[s == s.groupby(level=0).transform('max')]
    print (out)
    Year  month
    2003  2         9195.0
    2004  3        17334.0
    2005  5        11950.0
    Name: AMT, dtype: float64
    

    【讨论】:

    • 这里的最大值冲突的情况会发生什么?
    • @SouvikMondal - 最大值之间没有冲突,如果可能的话,只有解决方案应该是不同的
    【解决方案2】:

    您可以使用GroupBy + transform 和max。请注意,这给出了存在平局的任何年份的多个最大值。这可能是也可能不是您需要的。

    按照您的要求,可以分两步完成,首先求和,然后按年份计算最大值。

    df = pd.DataFrame({'year': [2003, 2003, 2003, 2004, 2004, 2004],
                       'month': [1, 2, 2, 1, 1, 2],
                       'AMT': [100, 200, 100, 100, 300, 100]})
    
    # STEP 1: sum by year + month
    df2 = df.groupby(['year', 'month']).sum().reset_index()
    
    # STEP 2: filter for max by year
    res = df2[df2['AMT'] == df2.groupby(['year'])['AMT'].transform('max')]
    
    print(res)
    
       year  month  AMT
    1  2003      2  300
    2  2004      1  400
    

    【讨论】:

    • 这有点不简单,因为我首先需要汇总“AMT”的总和,因为我每年都有数十万个“AMT”值。有什么方法可以将其适应您的代码?
    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2022-01-25
    • 2021-02-08
    • 2018-02-28
    • 2022-01-11
    相关资源
    最近更新 更多