Is there a way to easily rename the name of the 2nd column of Pandas DataFrame in the case below?
a = pd.DataFrame({'a':[1, 2], 'b':[3, 2]}).groupby('a', as_index=False).agg({'b':['sum', 'count']})
I tried the answer from this question but this dataframe has a multi-level column name (i.e. a.columns[1]
is ["b","sum"]
instead of a single string like "bsum"
). The following code won't work.
a.rename(columns={a.columns[1]:'new_name'})
For my case, it is not very easy to figure out the exact column name so I want to have a way to change the name of the column based on it's position.
Thanks in advance.