I'm trying to rename two columns after using a groupby
of two columns.
fun = {'Age':{'mean_age':'mean', 'median_age':'median'}}
groupbyClass2 = mc_response.groupby(['Country','GenderSelect']).agg(fun).reset_index()
groupbyClass2.columns = groupbyClass2.columns.droplevel(0)
The dataframe looks like the following:
mean_age median_age
0 Argentina Female 33.000000 33.0
1 Argentina Male 33.294118 32.0
2 Australia Female 35.000000 34.0
3 Australia Male 37.158416 36.0
Now I want to rename the first column to 'Country' and the second column to 'Gender'. I tried the following code however the two columns will both be renamed to 'Gender'. How can I fix this?
groupbyClass2.rename(columns = {groupbyClass2.columns[0]:'Country', groupbyClass2.columns[1]:'Gender'},inplace = True)