基于变长窗口的两列groupby滚动平均

2024-09-28 05:16:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

              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)。你知道吗


Tags: 模式价格解决方案price商品平均值gramwheat
2条回答

我想你应该为每年x APMC x商品分组,然后用.expanding().mean()来计算每组的滚动平均数。因为你的数据似乎是每月的,这将是一个滚动平均每月。你知道吗

样本数据

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

由于输出具有原始DataFrame的索引,因此如果需要,可以加入结果。你知道吗

我不知道你是否需要分组,但你可以:

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

但打了这个之后,我觉得你的“那一年的月数”有点不对劲。你知道吗

不管怎样,这不是你想要的,但解决了你的问题。你知道吗

相关问题 更多 >

    热门问题