Add GroupBy mean result as a new column in pandas
问题 I have a dataframe that gives upper and lower values of each indicator as follows df = pd.DataFrame( {'indicator': ['indicator 1', 'indicator 1', 'indicator 2', 'indicator 2'], 'year':[2014,2014,2015,2015], 'value type': ['upper', 'lower', 'upper', 'lower'], 'value':[12.3, 10.2, 15.4, 13.2] }, index=[1,2,3,4]) I want to remove the upper and lower values and replace that with the mean of two values. How can I do that? 回答1: You could groupby and transform by mean . df['value'] = df.groupby(