Resampling a pandas dataframe with multi-index containing timeseries

前端 未结 2 662
一个人的身影
一个人的身影 2020-12-19 08:52

apologies from creating what appears to be a duplicate of this question. I have a dataframe that is shaped more or less like the one below:

df_lenght = 240
d         


        
2条回答
  •  暖寄归人
    2020-12-19 09:25

    First let's define a resampler function:

    def resampler(x):    
        return x.set_index('datetime').resample('D').mean().rolling(window=2).mean()
    

    Then, we groupby job_id and apply the resampler function:

     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
    

    Let me know if this is what you are after.

提交回复
热议问题