I have the following DataFrame, every month numbers are not duplicates, it's from different years.
print(df)
Month Deviation
0 1 10
1 2 20
2 3 30
3 1 3
4 2 6
5 3 9
6 1 20
7 2 40
8 3 50
I need to get a mean value of 'Deviation' of a particular Month numbers from different years. I have only 3 months types (1,2,3), so only 3 mean values should be in df. Expecting the following:
Month Deviation Avrg
0 1 10 7.6
1 2 20 22
2 3 30 29.6
3 1 3 7.6
4 2 6 22
5 3 9 29.6
6 1 20 7.6
7 2 40 22
8 3 50 29.6
I'm trying to group by months and then get this mean value to a lambda function:
gr = df.groupby('Month')['Deviation'].mean().reset_index()
df.assign(Avrg=lambda x: gr.loc[df.loc[x, 'Month'],'Deviation'])
this is not working