【问题标题】:Yearly summations and monthly averages with PandasPandas 的年度总和和月平均值
【发布时间】:2018-09-13 06:37:27
【问题描述】:

我有一组按日期索引的数据。有没有一种简单的方法可以从此数据集中获得年度总数和月平均值?

                       a       b                  c              d          e
Statement Date                                                                  
2003-12-29         655.0   54.51           0.083221            0.0       4.70   
2004-01-28         978.0   82.69           0.084550            0.0       4.70   
2004-02-25         905.0   78.58           0.086829            0.0       4.70   
2004-03-29        1099.0   95.90           0.087261            0.0       4.70   
2004-04-28        1070.0   93.88           0.087738            0.0       4.70   
2004-05-26         656.0   57.99           0.088399            0.0       4.70   
2004-06-28         527.0   43.92           0.083340            0.0       4.70   
2004-07-28         399.0   32.79           0.082180            0.0       4.70   
2004-08-27         359.0   30.53           0.085042            0.0       4.70   
2004-09-28         381.0   34.76           0.091234            0.0       4.70   
2004-10-26         471.0   45.25           0.096072            0.0       4.70   
2004-11-24         967.0   85.99           0.088925            0.0       4.70   
2004-12-28        1175.0  101.49           0.086374            0.0       4.70   
2005-01-27         849.0   80.78           0.095147            0.0       4.70   
2005-02-24         641.0   61.24           0.095538            0.0       4.70   
2005-03-29         821.0   77.10           0.093910            0.0       4.70   
2005-04-27         647.0   64.49           0.099675            0.0       4.70   
2005-05-26         514.0   49.54           0.096381            0.0       4.70   
2005-06-28         411.0   39.78           0.096788            0.0       4.70   
2005-07-27         411.0   39.70           0.096594            0.0       4.70   
2005-08-29         834.0   83.20           0.099760            0.0       4.70   
2005-09-28         589.0   59.67           0.101307            0.0       4.70   
2005-10-26         476.0   52.29           0.109853            0.0       4.70   
2005-11-28         703.0   77.26           0.109900            0.0       4.70   
2005-12-28         758.0   90.35           0.119195            0.0       4.70   
2006-01-27         668.0   71.12           0.106467           99.0      10.54   
2006-02-24         830.0   88.17           0.106229           13.0       4.70   
2006-03-29         859.0   92.09           0.107206            0.0       4.70   
2006-04-26         557.0   59.41           0.106661            2.0       4.70   
2006-05-26         732.0   76.88           0.105027           27.0       4.70   

我想创建 a 列的年度总计以及平均每月使用量(即 2004 年 1 月、2005 年和 2006 年 1 月的平均 a 列值)。我试图使用熊猫石斑鱼,但无法让它发挥作用。如果可能的话,将新值输出到新数据框会很好。任何帮助表示赞赏。

如果有不清楚的地方请告诉我

【问题讨论】:

    标签: python-3.x pandas pandas-groupby


    【解决方案1】:

    我认为需要使用DatetimeIndex.month_nameDatetimeIndex.strftime 和聚合mean 在输出中正确排序CategoricalIndex

    cats = ['January','February','March','April','May','June','July','August',
              'September','October','November','December']
    
    idx = pd.CategoricalIndex(df.index.month_name(), categories=cats, ordered=True)
    #alternative solution
    #idx = pd.CategoricalIndex(df.index.strftime('%B'), categories=cats, ordered=True)
    df1 = df.groupby(idx).mean()
    print (df1)
                             a          b         c          d         e
    Statement Date                                                      
    January         831.666667  78.196667  0.095388  33.000000  6.646667
    February        792.000000  75.996667  0.096199   4.333333  4.700000
    March           926.333333  88.363333  0.096126   0.000000  4.700000
    April           758.000000  72.593333  0.098025   0.666667  4.700000
    May             634.000000  61.470000  0.096602   9.000000  4.700000
    June            469.000000  41.850000  0.090064   0.000000  4.700000
    July            405.000000  36.245000  0.089387   0.000000  4.700000
    August          596.500000  56.865000  0.092401   0.000000  4.700000
    September       485.000000  47.215000  0.096271   0.000000  4.700000
    October         473.500000  48.770000  0.102962   0.000000  4.700000
    November        835.000000  81.625000  0.099413   0.000000  4.700000
    December        862.666667  82.116667  0.096263   0.000000  4.700000
    

    DatetimeIndex.year 聚合sum

    df2 = df.groupby(df.index.year).sum()
    print (df2)
                         a       b         c      d      e
    Statement Date                                        
    2003             655.0   54.51  0.083221    0.0   4.70
    2004            8987.0  783.77  1.047944    0.0  56.40
    2005            7654.0  775.40  1.214048    0.0  56.40
    2006            3646.0  387.67  0.531590  141.0  29.34
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2021-12-08
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      相关资源
      最近更新 更多