【问题标题】:Year wise aggregation on the given condition in pandas大熊猫给定条件的年度聚合
【发布时间】:2020-06-21 23:44:48
【问题描述】:

我有一个如下所示的数据框。这是2016年12月至2018年11月两种保健品的销售数据。

 product     price      sale_date       discount
   A         50         2016-12-01      5
   A         50         2017-01-03      4
   B         200        2016-12-24      10
   A         50         2017-01-18      3
   B         200        2017-01-28      15
   A         50         2017-01-18      6
   B         200        2017-01-28      20
   A         50         2017-04-18      6
   B         200        2017-12-08      25
   A         50         2017-11-18      6
   B         200        2017-08-21      20
   B         200        2017-12-28      30
   A         50         2018-03-18      10
   B         300        2018-06-08      45
   B         300        2018-09-20      50
   A         50         2018-11-18      8
   B         300        2018-11-28      35

从上面我想准备下面的数据框

预期输出:

product    year     number_of_months     total_price total_discount number_of_sales
A          2016     1                    50            5            1
B          2016     1                    200           10           1
A          2017     12                   250           25           5
B          2017     12                   1000          110          5  
A          2018     11                   100           18           2
B          2018     11                   900           130          3

注意:请注意,数据从 2016 年 12 月到 2018 年 11 月。 所以 2016 年的月数是 1,2017 年我们有完整的数据,所以 12 个月和 2018 年我们有 11 个月。

【问题讨论】:

    标签: pandas pandas-groupby


    【解决方案1】:

    首先按年和product 聚合sum,然后按DataFrame.insertSeries.map 按月创建新列:

    df1 =(df.groupby(['product',df['sale_date'].dt.year], sort=False).sum().add_prefix('total_')
            .reset_index())
    
    df1.insert(2,'number_of_months', df1['sale_date'].map({2016:1, 2017:12, 2018:11}))
    print (df1)
    
      product  sale_date  number_of_months  total_price  total_discount
    0       A       2016                 1           50               5
    1       A       2017                12          250              25
    2       B       2016                 1          200              10
    3       B       2017                12         1000             110
    4       A       2018                11          100              18
    5       B       2018                11          900             130
    

    如果想要通过最小和最大日期时间使用动态字典:

    s = pd.date_range(df['sale_date'].min(), df['sale_date'].max(), freq='MS')
    
    d = s.year.value_counts().to_dict()
    print (d)
    {2017: 12, 2018: 11, 2016: 1}
    
    df1 = (df.groupby(['product',df['sale_date'].dt.year], sort=False).sum().add_prefix('total_')
            .reset_index())
    
    df1.insert(2,'number_of_months', df1['sale_date'].map(d))
    print (df1)
      product  sale_date  number_of_months  total_price  total_discount
    0       A       2016                 1           50               5
    1       A       2017                12          250              25
    2       B       2016                 1          200              10
    3       B       2017                12         1000             110
    4       A       2018                11          100              18
    5       B       2018                11          900             130
    

    对于绘图使用DataFrame.set_indexDataFrame.unstack

    df2 = (df1.set_index(['sale_date','product'])[['total_price','total_discount']]
             .unstack(fill_value=0))
    df2.columns = df2.columns.map('_'.join)
    print (df2)
              total_price_A  total_price_B  total_discount_A  total_discount_B
    sale_date                                                                  
    2016                  50            200                 5                10
    2017                 250           1000                25               110
    2018                 100            900                18               130
    
    df2.plot()
    

    编辑:

    df1 = (df.groupby(['product',df['sale_date'].dt.year], sort=False)
            .agg( total_price=('price','sum'),
                 total_discount=('discount','sum'),
                 number_of_sales=('discount','size'))
            .reset_index())
    
    df1.insert(2,'number_of_months', df1['sale_date'].map({2016:1, 2017:12, 2018:11}))
    print (df1)
      product  sale_date  number_of_months  total_price  total_discount  \
    0       A       2016               NaN           50               5   
    1       A       2017               NaN          250              25   
    2       B       2016               NaN          200              10   
    3       B       2017               NaN         1000             110   
    4       A       2018               NaN          100              18   
    5       B       2018               NaN          900             130   
    
       number_of_sales  
    0                1  
    1                5  
    2                1  
    3                5  
    4                2  
    5                3  
    

    【讨论】:

    • 是否可以在 python 中使用 plotly. x 轴 - 年份 y 轴 - total_price y 轴 - total_discount A 和 B 分开
    • @Danish 一个问题 - 年数是第一个和最后一个值的计数形式,如第二个解决方案或按年计算,如第一个解决方案?
    • 请帮助我使用 pyton 进行绘图。
    • @Danish - 所以情节中有 4 行?
    • 是的。我想将产品 A 的两条线图合二为一。产品 B 也是如此
    猜你喜欢
    • 2022-11-28
    • 2015-01-03
    • 1970-01-01
    • 2021-06-21
    • 2022-12-10
    • 2022-12-16
    • 1970-01-01
    • 2020-01-10
    相关资源
    最近更新 更多