I would like from a table with 2 columns Group and Value, to create a new column shift_n_value
This column should be the column Value but N shifted by group.
For example if shift by one, the output should be :
Group | Value | shift_n_value |
A | 2 | . |
A | 1 | 2 |
A | 3 | 1 |
A | 1 | 3 |
B | 4 | . |
B | 2 | 4 |
B | 0 | 2 |
If i shift twice:
Group | Value | shift_n_value |
A | 2 | . |
A | 1 | . |
A | 3 | 2 |
A | 1 | 1 |
B | 4 | . |
B | 2 | . |
B | 0 | 4 |
Basically it is the equivalent in python to : df['shift_n_value'] = df.groupby('group')['value'].shift(n)