Skip to content Skip to sidebar Skip to footer

How To Shift Entire Groups In Pandas Groupby

Given the following data: data = {'a' : [1,1,1,8,8,3,3,3,3,4,4] } df = pd.DataFrame(data) I would now like to shift the whole thing down by n groups, so that their current order i

Solution 1:

This is an interesting operation. I can think of an alternative way to do it with replace.

To shift by 1 group:

>>>df['b'] = df.a.shift()>>>x = df[df.a != df.b]>>>df.replace(*x.values.T)

Which gives the DataFrame:

     a   b
0NaNNaN1NaNNaN2NaNNaN31NaN4115816887888889381033

And we just want column a of this DataFrame:

desired_output_df = pd.DataFrame(_, columns=['a'])

To shift by more than one group, you just need to shift column b of x. If you want to shift by n groups, you need to shift x.b an additional n-1 times. Just insert the line

>>>x.b = x.b.shift(n-1)

after x = df[df.a != df.b] and then perform the df.replace(*x.values.T) step.

Post a Comment for "How To Shift Entire Groups In Pandas Groupby"