【问题标题】:Which month has the highest cumulative sum in multiindex pandas多指数熊猫中哪个月的累计总和最高
【发布时间】:2021-02-13 12:38:25
【问题描述】:

我有这个MultiIndexpandas 数据框:

            chamber_temp
month day          
1     1    0.000000
      2    0.005977
      3    0.001439
      4   -0.000119
      5    0.000514
            ...
12    27   0.001799
      28   0.002346
      29  -0.001815
      30   0.001102
      31  -0.004189

我想得到的是哪个月的 cumsum() 最高。 我想要做的是每个月应该有 1 个值,这将给我该月 day 的所有值的累积总和,这就是我试图寻求帮助的问题。

【问题讨论】:

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


    【解决方案1】:

    当有MultiIndex 时,您可以利用Series.sum 中的level 参数来避免在这种情况下使用groupby。

    df['champer_temp'].sum(level=0).idxmax()
    

    【讨论】:

      【解决方案2】:

      请尝试

      df.groupby('month')['chamber_temp'].sum().idxmax()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        • 2018-10-30
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 2017-10-02
        相关资源
        最近更新 更多