【问题标题】:Cumulative monthly sum with reset to zero at the beginning of each new month in pandas在熊猫每个新月初重置为零的累积每月总和
【发布时间】:2021-01-04 03:42:00
【问题描述】:

我有一个包含每日数据的 pandas 数据框

Date          Value
2020-01-01    1780.2
2020-01-02    1783.3
2020-01-05    1781.5
...
2020-02-01    1816.0
2020-02-02    1810.4
...

每个月的每一天并不总是有一个值,因此可能会丢失某些天,因此 timedelta 并不总是 1 天。

我想要做的是简单地在每个月内取一个累积总和,然后在下月初将总和重置为零,所以结果如下所示

Date          Value    Cumulative Value
2020-01-01    1780.2   1780.2
2020-01-02    1783.3   3563.5
2020-01-05    1781.5   5345.0
...
2020-02-01    1816.0   1816.0
2020-02-02    1810.4   3626.4
...

我找到了this post,它解释了如何按月获得累计金额。但我需要的是在每个月初将累积总和重置为零。我该怎么做?

【问题讨论】:

  • groupby 按月然后sum 每组

标签: python pandas time-series cumulative-sum


【解决方案1】:

我假设 Date 列属于 datetime 类型。如果没有,请转换它。

据我了解,您希望不仅仅按月份分组(例如放在一起 1 月、2 月等所有年份的数据(如其他 2 个 答案)),但分组应该按年和月(你想要 从 0 下月初开始)。

要以这种方式计算新列,请运行:

df['Cumulative Value'] = df.groupby(pd.Grouper(key='Date', freq='M')).Value.cumsum()

您的数据样本的结果是:

        Date   Value  Cumulative Value
0 2020-01-01  1780.2            1780.2
1 2020-01-02  1783.3            3563.5
2 2020-01-05  1781.5            5345.0
3 2020-02-01  1816.0            1816.0
4 2020-02-02  1810.4            3626.4

【讨论】:

    【解决方案2】:
    import pandas as pd
    start='2020-01-01' 
    end='2020-12-31'
    df = pd.DataFrame({"Date": pd.date_range(start, end)})
    df['qty']=1
    df = df.groupby('Date').qty.sum()
    print(df.head())
    df=df.groupby(df.index.month).cumsum().reset_index()
    print(df.head(45))
    

    输出:

    Date
    2020-01-01    1
    2020-01-02    1
    2020-01-03    1
    2020-01-04    1
    2020-01-05    1
    Name: qty, dtype: int64
             Date  qty
    0  2020-01-01    1
    1  2020-01-02    2
    2  2020-01-03    3
    3  2020-01-04    4
    4  2020-01-05    5
    5  2020-01-06    6
    6  2020-01-07    7
    7  2020-01-08    8
    8  2020-01-09    9
    9  2020-01-10   10
    10 2020-01-11   11
    11 2020-01-12   12
    12 2020-01-13   13
    13 2020-01-14   14
    14 2020-01-15   15
    15 2020-01-16   16
    16 2020-01-17   17
    17 2020-01-18   18
    18 2020-01-19   19
    19 2020-01-20   20
    20 2020-01-21   21
    21 2020-01-22   22
    22 2020-01-23   23
    23 2020-01-24   24
    24 2020-01-25   25
    25 2020-01-26   26
    26 2020-01-27   27
    27 2020-01-28   28
    28 2020-01-29   29
    29 2020-01-30   30
    30 2020-01-31   31
    31 2020-02-01    1
    32 2020-02-02    2
    33 2020-02-03    3
    34 2020-02-04    4
    35 2020-02-05    5
    36 2020-02-06    6
    37 2020-02-07    7
    38 2020-02-08    8
    39 2020-02-09    9
    40 2020-02-10   10
    41 2020-02-11   11
    42 2020-02-12   12
    43 2020-02-13   13
    44 2020-02-14   14
    

    【讨论】:

      【解决方案3】:

      groupy() 提取的月份,transform('cumsum')

       df['Cumulative Value']=df.groupby(pd.to_datetime(df.Date).dt.month).transform('cumsum')
      
      
      
           Date         Value        Cumulative Value
      0  2020-01-01  1780.2            1780.2
      1  2020-01-02  1783.3            3563.5
      2  2020-01-05  1781.5            5345.0
      3  2020-02-01  1816.0            1816.0
      4  2020-02-02  1810.4            3626.4
      

      【讨论】:

        【解决方案4】:

        如果多个年份按月份按Series.dt.to_period 分组的解决方案:

        df['Cumulative Value'] = df.groupby(df['Date'].dt.to_period('m'))['Value'].cumsum()
        

        如果可能只有一年的解决方案,请使用Series.dt.month:

        df['Cumulative Value'] = df.groupby(df['Date'].dt.month)['Value'].cumsum() 
        

        默认情况下累积总和重置为0,因此无需为此添加代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-28
          • 1970-01-01
          • 1970-01-01
          • 2021-10-11
          • 1970-01-01
          • 1970-01-01
          • 2023-01-23
          • 1970-01-01
          相关资源
          最近更新 更多