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
You can do it the way you intended by tweaking your code in the following way:
o2 = o2.set_index(['YEAR', 'daytype', 'hourtype'])
o2['premium'] = o2.groupby(level=['YEAR', 'daytype', 'hourtype'])['option_value'].mean()
Why the original error? As explained by John Galt, the data coming out of groupby().mean() is not the same shape (length) as the original DataFrame.
Pandas can handle this cleverly if you first start with the 'grouping columns' in the index. Then it knows how to propogate the mean data correctly.
John's solution follows the same logic, because groupby naturally puts the grouping columns in the index during execution.