if I have a dataframe like this:
category name index
A A11 1
A A12 1
A A13 1
A A21 2
A A22 2
A A23 2
...
B B11 1
B B21 2
...
I want to first group the dataframe by the category and index, and inside each group I want to shift the name
column by the value of index (reversed shifting) - 1.
So the result would be like:
category name index
A A11 1 # Do not shift anything because index is 1
A A12 1
A A13 1
A A22 2 # Shift -1 for anything in (A, 2) group because index here is 2
A A23 2
A A24 2
...
B B11 1
B NA 2 # at the end of each group it should be NA
I have tried to use .groupby() and then .apply() but it seems that there is no easy way to join the result back to the dataframe. It seems that the apply method can only return a series. How could I achieve this using the apply method?