python-3.x - 在多年数据集中找到平均每个月的增长百分比
问题描述
我有一个来自 yahoo Finance 的数据集,我试图按月分组(我已经这样做了),然后绘制该月在年度周期中的平均百分比变化(数据可以追溯到 21 年)。例如,我将有 21 个 1 月的平均百分比增加/减少 1 月的总和,21 月 21 月的平均百分比增加/减少 2 月的总和,等等。
在玩了大约 2 个小时之后,我无法将它正确地聚集在一起,这让我发疯了。我曾尝试使用pd.Grouper
和pd.groupby
和日期时间的变体来隔离月份,或者给月份分配一个指定的数字来分组,但我没有运气。
我正在寻找的预期结果应该是这样的(~ 是日期时间 YYYYMMDD 格式的占位符)
Adj Close (%) Month (int)
Date 8.5 1
~ -3.2 2
~ 7.18 3
~ -1.8% 4
~ ... ...
用来pd.to_clipboard
创建这个数据(应该可以pd.read_clipboard
用来读取)
月度数据样本:(日期是索引列,所以这是功能时间序列数据)
Adj Close
Date
1999-09-30 NaN
1999-10-31 6.253954
1999-11-30 1.906186
1999-12-31 5.784389
2000-01-31 -5.090355
2000-02-29 -2.010808
2000-03-31 9.671983
2000-04-30 -3.079576
2000-05-31 -2.191505
2000-06-30 2.393355
2000-07-31 -1.634128
2000-08-31 6.069910
2000-09-30 -5.348297
2000-10-31 -0.494949
2000-11-30 -8.006861
2000-12-31 0.405345
2001-01-31 3.463658
2001-02-28 -9.229074
2001-03-31 -6.420471
2001-04-30 7.681436
解决方案
您是否正在寻找:
dti = pd.date_range('1999', '2020', freq='M', name='Date')
df1 = pd.DataFrame({'Adj Close': np.random.randint(1, 30, len(dti))}, index=dti)
out = df1.groupby(df1.index.month)['Adj Close'] \
.apply(lambda x: x.pct_change().mean()) \
.rename_axis('Month').reset_index()
输出:
>>> out
Month Adj Close
0 1 1.185047
1 2 2.057344
2 3 1.116738
3 4 1.531147
4 5 0.981474
5 6 2.025258
6 7 0.914078
7 8 0.301812
8 9 0.710584
9 10 1.498942
10 11 1.910080
11 12 1.643428
推荐阅读
- coldfusion - 如何从不同文件夹中的文件中使用组件
- r - R中不同长度向量的组合
- google-apps-script - Google Apps 脚本:有没有办法从单个单元格写入值数组?
- python - 有没有办法让在 with 语句中调用的函数隐式获取 with 对象而不将其传递给另一个函数?
- mysql - 如果与内容匹配,则从另一个表中选择列
- javascript - 如何处理图像背景的加载状态?
- git - 在构建管道期间出现 azure repo taggine 错误
- c# - 动态创建的表达式
- r - 计算r中数据框的每日平均值
- javascript - Blazor webassembly:canvas.toDataUrl()?