How to speed up Pandas multilevel dataframe shift by group?

后端 未结 4 1872
离开以前
离开以前 2021-01-22 03:36

I am trying to shift the Pandas dataframe column data by group of first index. Here is the demo code:

 In [8]: df = mul_df(5,4,3)

In [9]: df
Out[9]:
                    


        
4条回答
  •  执念已碎
    2021-01-22 04:12

    How about shift the total DataFrame object and then set the first row of every group to NaN?

    dfs = df.shift(1)
    dfs.iloc[df.groupby(level=0).size().cumsum()[:-1]] = np.nan
    

提交回复
热议问题