通过创建似乎与this question重复的内容道歉。我的数据框形状或多或少与下面的形状相同:
df_lenght = 240
df = pd.DataFrame(np.random.randn(df_lenght,2), columns=['a','b'] )
df['datetime'] = pd.date_range('23/06/2017', periods=df_lenght, freq='H')
unique_jobs = ['job1','job2','job3',]
job_id = [unique_jobs for i in range (1, int((df_lenght/len(unique_jobs))+1) ,1) ]
df['job_id'] = sorted( [val for sublist in job_id for val in sublist] )
df.set_index(['job_id','datetime'], append=True, inplace=True)
print(df[:5])
返回:
a b
job_id datetime
0 job1 2017-06-23 00:00:00 -0.067011 -0.516382
1 job1 2017-06-23 01:00:00 -0.174199 0.068693
2 job1 2017-06-23 02:00:00 -1.227568 -0.103878
3 job1 2017-06-23 03:00:00 -0.847565 -0.345161
4 job1 2017-06-23 04:00:00 0.028852 3.111738
我需要重新取样df['a']
以获得每日滚动均值,即应用.resample('D').mean().rolling(window=2).mean()
。
我尝试了两种方法:
1 - 按照建议here
进行拆散和堆叠df.unstack('job_id','datetime').resample('D').mean().rolling(window=2).mean().stack('job_id', 'datetime')
这会返回错误
2 - 按建议使用pd.Grouper
here
level_values = df.index.get_level_values
result = df.groupby( [ level_values(i) for i in [0,1] ] + [ pd.Grouper(freq='D', level=2) ] ).mean().rolling(window=2).mean()
这不会返回错误但似乎没有适当地重新取样/分组df。结果似乎包含每小时数据点,而不是每天:
print(result[:5])
a b
job_id datetime
0 job1 2017-06-23 NaN NaN
1 job1 2017-06-23 0.831609 1.348970
2 job1 2017-06-23 -0.560047 1.063316
3 job1 2017-06-23 -0.641936 -0.199189
4 job1 2017-06-23 0.254402 -0.328190
答案 0 :(得分:5)
首先让我们定义一个重采样器功能:
def resampler(x):
return x.set_index('datetime').resample('D').mean().rolling(window=2).mean()
然后,我们通过job_id分组并应用重新采样器功能:
df.reset_index(level=2).groupby(level=1).apply(resampler)
Out[657]:
a b
job_id datetime
job1 2017-06-23 NaN NaN
2017-06-24 0.053378 0.004727
2017-06-25 0.265074 0.234081
2017-06-26 0.192286 0.138148
job2 2017-06-26 NaN NaN
2017-06-27 -0.016629 -0.041284
2017-06-28 -0.028662 0.055399
2017-06-29 0.113299 -0.204670
job3 2017-06-29 NaN NaN
2017-06-30 0.233524 -0.194982
2017-07-01 0.068839 -0.237573
2017-07-02 -0.051211 -0.069917
如果这就是您的目的,请告诉我。
答案 1 :(得分:3)
IIUC,您希望按job_id
和(每日)datetime
进行分组,并希望忽略DataFrame索引的第一级。因此,而不是按
( [ level_values(i) for i in [0,1] ] + [ pd.Grouper(freq='D', level=2) ] )
你想要分组
[df.index.get_level_values(1), pd.Grouper(freq='D', level=2)]
import numpy as np
import pandas as pd
np.random.seed(2017)
df_length = 240
df = pd.DataFrame(np.random.randn(df_length,2), columns=['a','b'] )
df['datetime'] = pd.date_range('23/06/2017', periods=df_length, freq='H')
unique_jobs = ['job1','job2','job3',]
job_id = [unique_jobs for i in range (1, int((df_length/len(unique_jobs))+1) ,1) ]
df['job_id'] = sorted( [val for sublist in job_id for val in sublist] )
df.set_index(['job_id','datetime'], append=True, inplace=True)
grouped = df.groupby([df.index.get_level_values(1), pd.Grouper(freq='D', level=2)])
result = grouped.mean().rolling(window=2).mean()
print(result)
产量
a b
job_id datetime
job1 2017-06-23 NaN NaN
2017-06-24 -0.203083 0.176141
2017-06-25 -0.077083 0.072510
2017-06-26 -0.237611 -0.493329
job2 2017-06-26 -0.297775 -0.370543
2017-06-27 0.005124 0.052603
2017-06-28 0.226142 -0.015584
2017-06-29 -0.065595 0.210628
job3 2017-06-29 -0.186865 0.347683
2017-06-30 0.051508 0.029909
2017-07-01 0.005341 0.075378
2017-07-02 -0.027131 0.132192