I have been working on sport analysis especially soccer, I am calculating week rank of the teams. I want to track their position changes. Here is my sample data that replicate my problem.
df = pd.DataFrame()
df ['Season'] = ['1314','1314','1314','1314','1314','1314','1314','1314','1314','1415','1415','1415','1415','1415','1415','1415','1415','1415']
df ['Team'] = ['A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B','C']
df ['GW'] = [1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3]
df['Position'] = [1,2,3,3,1,2,2,3,1,2,1,3,2,1,3,3,2,1]
SO I have all the weekly position etc. To track the position changes, I will sort dataframe first as follow:
df = df.sort_values (['Season','Team'])
after sorting, I apply diff() function to get the change as follow:
v = df.Position.diff()
v[0] = 0
df ['Position_change']= v
I wanted to track the changes for each GW.
In my data frame i have 3 GWs. I want all GW1 for each team to be 0 at position change.
The second issue is the changes is taken between two teams (which i do not want) if I use diff()
You can see in the attached picture:

Can anyone advice on it?