I have a DataFrame that looks like this
df = pd.DataFrame({'user' : ['A', 'A', 'A', 'B', 'B', 'B','B'],
'attritube1' : [0,1,1,1,0,2,9],
'attritube2':[1,2,3,3,0,0,1]})
print(df)
attritube1 attritube2 user
0 0 1 A
1 1 2 A
2 1 3 A
3 1 3 B
4 0 0 B
5 2 0 B
6 9 1 B
I would like to slice the data with a rolling window of length K for every user and create a new data set. For example, if K = 2, then I would like to get
attritube1 attritube2 user
0 0 1 A
1 1 2 A
---------------------------------
2 1 2 A
3 1 3 A
---------------------------------
4 1 3 B
5 0 0 B
---------------------------------
6 0 0 B
7 2 0 B
--------------------------------
8 2 0 B
9 9 1 B
Similarly, if K = 3, then the new data frame should be
attritube1 attritube2 user
0 0 1 A
1 1 2 A
2 1 3 A
--------------------------------
3 1 3 B
4 0 0 B
5 2 0 B
--------------------------------
6 0 0 B
7 2 0 B
8 9 1 B
We can assume that for all users, the number of rows >= K. Thanks!
Edit: Want to clarify that I want to repeat the rolling window procedure for every user (A,B in the toy example).