【发布时间】:2021-06-17 17:43:28
【问题描述】:
在 Pandas 中,我想创建列,表示从 11 月开始到明年 10 月结束的季节(例如旅游季节)。
这是我的sn-p:
from numpy import random
import pandas as pd
np.random.seed(0)
df = pd.DataFrame({
'date': pd.date_range('1990-01-01', freq='M', periods=12),
'travel_2016': random.randint(10, size=(12)),
'travel_2017': random.randint(10, size=(12)),
'travel_2018': random.randint(10, size=(12)),
'travel_2019': random.randint(10, size=(12)),
'travel_2020': random.randint(10, size=(12))})
df['month_date'] = df['date'].dt.strftime('%m')
df = df.drop(columns = ['date'])
我正在尝试这种方法pandas groupby by customized year, e.g. a school year 在使用两种解决方案“反透视”表后,我失败了。对我来说,为将来的操作保留数据透视表会更容易。
我想要的输出是这样的:
season_2016/2017 season_2017/2018 season_2018/2019 season_2019/2020 month_date
0 8 7 7 4 11
1 0 1 4 8 12
2 1 4 5 9 01
3 8 3 5 7 02
4 4 7 8 3 03
5 6 8 4 4 04
6 5 8 3 1 05
7 7 0 1 1 06
8 1 2 1 3 07
9 8 9 7 5 08
10 7 7 7 8 09
11 9 1 4 0 10
非常感谢!
【问题讨论】:
-
预期数据帧中的值是基于输入数据帧还是虚构的?你能解释一下 1 季背后的逻辑吗?