我有一个如下所示的数据集:
month year value
1 2019 20
2 2019 13
3 2019 10
4 2019 20
5 2019 13
6 2019 10
7 2019 20
8 2019 13
9 2019 10
10 2019 20
11 2019 13
12 2019 10
1 2020 20
2 2020 13
3 2020 10
4 2020 40
请假设每个月和一年发生多次,而且还有更多的列。我想要创建的是6个月窗口中的多个数据文件。我不想有聚合。分区数据集应包括以下标准中的数据。请帮我照看熊猫。我知道简单的方法是手动使用条件来选择数据。但我想一次做这个手术会有更有效的方法。
month 1-6 year 2019
month 2-7 year 2019
month 3-8 year 2019
month 4-9 year 2019
month 5-10 year 2019
month 6-11 year 2019
month 7-12 year 2019
month 8-1 year 2019,2020
month 9-2 year 2019,2020
month 10-3 year 2019,2020
month 11-3 year 2019,2020
到目前为止我尝试过的:
for i, j in zip(range(1,12), range(6,13)):
print(i,j) # this is for 2019
我可以用这个i和j在几个月内把它插上,在2020年也能重复同样的情况。但是有一种更好的方法,可以很容易地创建一个数据格式列表。
发布于 2020-04-22 05:46:58
使用datetime索引和pd.Grouper,您可以按照以下步骤进行操作
df = pd.DataFrame(np.random.randn(12,3),
index = pd.date_range(pd.Timestamp.now(), periods = 12),
)
df_grouped = df.groupby(pd.Grouper(freq = "6M"))
[df_grouped.get_group(x) for x in df_grouped.groups]
https://stackoverflow.com/questions/61357101
复制相似问题