不确定我是否做错了什么(Pandas 1.2.5):
ids = pd.DataFrame(data=range(10), columns=['Id'])
dt = pd.DataFrame(pd.date_range('2021-01-01', '2021-01-10', freq='D'), columns=['Date'])
df = ids.merge(dt, how='cross')
df['Val'] = np.random.randint(1,10, size=len(df))
df.set_index(['Id', 'Date'], inplace=True)
df['Val'].groupby('Id').rolling(window=3).mean()我希望结果包含日期列(否则为什么要计算滚动平均值?)但是日期不在那里:
Id
0 NaN
0 NaN
0 2.333333
0 3.333333
0 3.666667
...
9 5.000000
9 4.000000
9 5.000000
9 5.333333
9 6.000000
Name: Val, Length: 100, dtype: float64我遗漏了什么?
而且,df['Val'].reset_index('Id').groupby('Id').rolling(window=3).mean()似乎以某种方式工作,但返回Id作为数据列和索引列,即使在groupby中传递了as_index=False。非常奇怪!
Id Val
Id Date
0 2021-01-01 NaN NaN
2021-01-02 NaN NaN
2021-01-03 0.0 7.000000
2021-01-04 0.0 6.333333
2021-01-05 0.0 4.666667
... ... ... ...发布于 2021-07-01 02:47:46
我觉得这样会更干净一点,
ids = pd.DataFrame(data=range(10), columns=['Id'])
dt = pd.DataFrame(pd.date_range('2021-01-01', '2021-01-10', freq='D'), columns=['Date'])
df = ids.merge(dt, how='cross')
df['Val'] = np.random.randint(1,10, size=len(df))
df.set_index(['Id'], inplace=True)
df.groupby(['Id']).rolling(window=3,on='Date').mean()#.head(60)唯一的变化是不在索引中包含'Date‘,而是滚动on='Date'
https://stackoverflow.com/questions/68199909
复制相似问题