Python 滚动平均值使用两列的groupby,窗口大小在中为可变长度

Python 滚动平均值使用两列的groupby,窗口大小在中为可变长度,python,pandas,group-by,Python,Pandas,Group By,我想计算每年按(APMC,商品)分组的modal_价格的滚动平均值,窗口长度为当年的月数。根据我的解决方案,我得到了所有的Nan。数据集如下所示: APMC | Commodity | qtl _weight| min_price | max_price | modal_price | district_name | Year | Month date 2014-12-01 Akole bajri 40 13

我想计算每年按(APMC,商品)分组的modal_价格的滚动平均值,窗口长度为当年的月数。根据我的解决方案,我得到了所有的Nan。数据集如下所示:

              APMC |   Commodity  | qtl _weight| min_price | max_price | modal_price | district_name | Year | Month
date
2014-12-01  Akole   bajri            40              1375        1750      1563          Ahmadnagar  2014   12
2014-12-01  Akole   paddy-unhusked   346             1400        1800      1625          Ahmadnagar  2014   12
2014-12-01  Akole   wheat            55              1500        1900       1675         Ahmadnagar  2014   12
2014-12-01  Akole   bhagar/vari      59              2000        2600       2400         Ahmadnagar  2014   12
2014-12-01  Akole   gram              9              3200        3300       3235         Ahmadnagar  2014   12
2014-12-01  Jamkhed cotton           44199           3950        4033       3991         Ahmadnagar  2014   12
2014-12-01  Jamkhed bajri            846             1300        1488       1394         Ahmadnagar  2014   12
2014-12-01  Jamkhed wheat(husked)    155             1879        2231       2055         Ahmadnagar  2014   12
2014-12-01  Kopar   gram             421             1983        2698       2463         Ahmadnagar  2014   12
2014-12-01  Kopar   greengram         18             6734        7259       6759         Ahmadnagar  2014   12
2014-12-01  Kopar   soybean          1507            2945        3247       3199         Ahmadnagar  2014   12
2016-11-01  Sanga   wheat(husked)    222             1730        2173       1994         Ahmadnagar  2016   11

每个APMC有6万行,商品集群在三年内有不同的月数(201420152016)。

我不知道您是否需要进行分组,但您可以只进行以下操作:

out = {}
for APMC_ in df.APMC.unique():
   for Commodity_ in df.Commodity.unique():
       for year_ in set(df.index.year):

           temp = df[(df.APMC==APMC_) & (df.Commodity==Commodity_) & (df.index.year==year_) ].copy()

           n_months = temp.shape[0]...

           out[APMC_ + Commodity_ + str(year)] = temp.mean() # or whatever
但打了这个之后,我觉得你的“那一年的月数”可能有点不适


无论如何,这并不是你想要的,但解决了你的问题。

我想你应该为每年的APMC x商品分组,然后使用
.expansing().mean()
计算每组的滚动平均数。由于您的数据似乎是月度数据,因此这将是每个月的滚动平均值

样本数据 代码 输出
由于输出具有原始
数据帧的索引,因此如果需要,您可以加入结果。

您是否可以为每年划分不同的dfs?仍然获得Nan的。我如何解释可变窗口大小?i、 e该年数据集中存在的月数?无法停止运行。正如我所说的,共有6万行,执行代码需要花费很长时间
import pandas as pd
import numpy as np

np.random.seed(123)
df = pd.DataFrame({'Date': ['2014-11-01','2014-11-01','2014-11-01',
                            '2014-12-01','2014-12-01','2014-12-01',
                            '2015-01-01','2015-01-01','2015-01-01'],
                   'APMC': np.tile(['Akole', 'Jamkhed', 'Kopar'], 3),
                   'Commodity': np.tile(['wheat', 'cotton', 'gram'], 3),
                   'modal_price': np.random.randint(1000,2000,9)})

df['Date'] = pd.to_datetime(df.Date)
df = df.set_index('Date')

#               APMC Commodity  modal_price
#Date                                      
#2014-11-01    Akole     wheat         1510
#2014-11-01  Jamkhed    cotton         1365
#2014-11-01    Kopar      gram         1382
#2014-12-01    Akole     wheat         1322
#2014-12-01  Jamkhed    cotton         1988
#2014-12-01    Kopar      gram         1098
#2015-01-01    Akole     wheat         1742
#2015-01-01  Jamkhed    cotton         1017
#2015-01-01    Kopar      gram         1595
df = df.sort_index()
df.assign(Year=df.index.year).groupby(['Year', 'APMC', 'Commodity']).modal_price.expanding().mean()
Year  APMC     Commodity  Date      
2014  Akole    wheat      2014-11-01    1510.0
                          2014-12-01    1416.0
      Jamkhed  cotton     2014-11-01    1365.0
                          2014-12-01    1676.5
      Kopar    gram       2014-11-01    1382.0
                          2014-12-01    1240.0
2015  Akole    wheat      2015-01-01    1742.0
      Jamkhed  cotton     2015-01-01    1017.0
      Kopar    gram       2015-01-01    1595.0
Name: modal_price, dtype: float64