How do you fill NaN with mean of a subset of a group?
问题 I have a data frame with some values by year and type . I want to replace all NaN values in each year with the mean of values in that year with a specific type. I would like to do this in the most elegant way possible. I'm dealing with a lot of data so less computation would be good as well. Example: df =pd.DataFrame({'year':[1,1,1,2,2,2], 'type':[1,1,2,1,1,2], 'val':[np.nan,5,10,100,200,np.nan]}) I want ALL nan's regardless of their type to be replaced with their respective year mean of all