Skip to main content
 首页 » 编程设计

python之Pandas groupby datetime,获取数量和价格

2025年04月02日8TianFang

我正在尝试使用 pandas 按给定日期的订阅类型对订阅者进行分组,并获取当天订阅类型的平均价格。我的数据类似于:

Sub_Date             Sub_Type    Price  
2011-03-31 00:00:00  12 Month    331.00 
2012-04-16 00:00:00  12 Month    334.70 
2013-08-06 00:00:00  12 Month    344.34 
2014-08-21 00:00:00  12 Month    362.53 
2015-08-31 00:00:00  6 Month     289.47 
2016-09-03 00:00:00  6 Month     245.57 
2013-04-10 00:00:00  4 Month     148.79 
2014-03-13 00:00:00  12 Month    348.46 
2015-03-15 00:00:00  12 Month    316.86 
2011-02-09 00:00:00  12 Month    333.25 
2012-03-09 00:00:00  12 Month    333.88 
... 
2013-04-03 00:00:00  12 Month    318.34 
2014-04-15 00:00:00  12 Month    350.73 
2015-04-19 00:00:00  6 Month     291.63 
2016-04-19 00:00:00  6 Month     247.35 
2011-02-14 00:00:00  12 Month    333.25 
2012-05-23 00:00:00  12 Month    317.77 
2013-05-28 00:00:00  12 Month    328.16 
2014-05-31 00:00:00  12 Month    360.02 
2011-07-11 00:00:00  12 Month    335.00 
... 

我正在寻找类似的东西:

Sub_Date             Sub_type    Quantity  Price   
2011-03-31 00:00:00  3 Month     2         125.00 
                     4 Month     0         0.00     # Promo not available this month 
                     6 Month     1         250.78        
                     12 Month    2         334.70 
2011-04-01 00:00:00  3 Month     2         125.00 
                     4 Month     2         145.00      
                     6 Month     0         250.78        
                     12 Month    0         334.70 
2013-04-02 00:00:00  3 Month     1         125.00 
                     4 Month     3         145.00     
                     6 Month     0         250.78        
                     12 Month    1         334.70 
... 
2015-06-23 00:00:00  3 Month     4         135.12 
                     4 Month     0         0.00     # Promo not available this month 
                     6 Month     0         272.71        
                     12 Month    3         354.12 
... 

我只能获得给定日期的 Sub_Type 总数。

df.Sub_Date.groupby([df.Sub_Date.values.astype('datetime64[D]')]).size() 

这是一个不错的开始,但并不是真正需要的。我看过 groupby pandas 网站上的文档,但我无法获得我想要的输出。

请您参考如下方法:

我想你需要aggregate通过 mean 和 size 然后通过 unstack 添加缺失值与 stack .

此外,如果需要更改级别 Sub_Type 的顺序,请使用 ordered categorical .

#generating all months ('1 Month','2 Month'...'12 Month') 
cat = [str(x) + ' Month' for x in range(1,13)] 
df.Sub_Type = df.Sub_Type.astype('category', categories=cat, ordered=True) 
 
df1 = df.Price.groupby([df.Sub_Date.values.astype('datetime64[D]'), df.Sub_Type]) 
        .agg(['mean', 'size']) 
        .rename(columns={'size':'Quantity','mean':'Price'}) 
        .unstack(fill_value=0) 
        .stack() 
 
print (df1) 
                      Price  Quantity 
           Sub_Type                   
2011-02-09 4 Month     0.00         0 
           6 Month     0.00         0 
           12 Month  333.25         1 
2011-02-14 4 Month     0.00         0 
           6 Month     0.00         0 
           12 Month  333.25         1 
2011-03-31 4 Month     0.00         0 
           6 Month     0.00         0 
           12 Month  331.00         1