计算每个俱乐部时间序列数据的季节性和趋势

2024-09-28 05:22:48 发布

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

我有这个时间序列数据,现在我想用“模态价格”计算每个APMC和商品集群的趋势季节性类型(乘法或加法)。数据集大约有60000行这样的行,APMC和Cluster是相同的,但是日期在变化。数据集如下:

             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

现在我尝试使用pivot表(APMC,Commodity和date作为索引),但这无助于计算每个集群(APMC,Commodity)的平均值(计算趋势)。我只需要知道如何使用“modal_price”计算每个集群(APMC,Commodity)的平均值,并将其添加到dataframe/pivot表中。在


Tags: 数据date集群price趋势grammodalcommodity
1条回答
网友
1楼 · 发布于 2024-09-28 05:22:48

也许groupby可以为你提供趋势所需的信息,然后转换将使你能够将其投影回同一个索引上? 比如:

# group by your cluster
g = df.groupby(["Year", "APMC", "Commodity"])
# determine the trend per cluster but finalise back into original diimensions
trend = g.modal_price.transform(lambda x: x.mean())
df["trend"] = trend

相关问题 更多 >

    热门问题