lag function in pandas 1

lag function in pandas

df['Data_lagged'] = df.groupby(['Group'])['Data'].shift(1)

Here is what the above code is Doing:
1. Create a new column called ‘Group’ that is the same as the ‘Group’ column.
2. Create a new column called ‘Data_lagged’ that is the same as the ‘Data’ column, but shifted by 1.

Similar Posts