[英]How to reshape a multi-indexed dataframe?
Here I have a large dataframe (Nx1, it has thousands of stocks, here I just make it to be 4)这里我有一个大的dataframe(Nx1,它有数千个库存,这里我只是让它成为4个)
ds code close
20160101 001 1.5
002 1.12
003 1.21
005 12.4
20160102 001 2.3
002 3.4
003 5.4
005 7.2
``` ``` ```
20201231 001 14.5
002 2.2
003 3.3
005 24.5
My purpose is to arbitrarily select a time period within the dataset, and calculate the average close of each stock in 30 days.我的目的是在数据集中任意一个时间段 select,计算每只股票在 30 天内的平均收盘价。 For example, the codes should be able to calculate the average close in 30 days of each stock from 20170101 to 20191231.例如,代码应该能够计算从 20170101 到 20191231 的每只股票在 30 天内的平均收盘价。
So I would like to split the large dataframe by "ds" and reform those splited dataframes to the following form, that it might be easier to reach my goal.因此,我想将大型 dataframe 拆分为“ds”,并将这些拆分的数据帧改成以下形式,这样可能更容易达到我的目标。 But I don't know if there is any easy way to get there?但不知道有没有什么简单的方法可以到达那里?
code 20160101 20160102 .... 20201231
001 1.5 2.3 .... 14.5
002 1.12 3.4 .... 2.2
003 1.21 5.4 .... 3.3
005 12.4 7.2 .... 24.5
Use Series.unstack
with select close
for Series
( ds
and code
are levels of MultiIndex
):使用Series.unstack
和 select close
Series
( ds
和code
是MultiIndex
的级别):
df1 = df['close'].unstack(level=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.