I'm trying to create a new column which returns the mean of values from an existing column in the same df. However the mean should be computed based on a grouping in three other columns.
Out[184]:
YEAR daytype hourtype scenario option_value
0 2015 SAT of_h 0 0.134499
1 2015 SUN of_h 1 63.019250
2 2015 WD of_h 2 52.113516
3 2015 WD pk_h 3 43.126513
4 2015 SAT of_h 4 56.431392
I basically would like to have a new column 'mean' which compute the mean of "option value", when "YEAR", "daytype", and "hourtype" are similar.
I tried the following approach but without success ...
In [185]: o2['premium']=o2.groupby(['YEAR', 'daytype', 'hourtype'])['option_cf'].mean()
TypeError: incompatible index of inserted column with frame index