Home > front end >  Pandas rename column by position with multilevel column name
Pandas rename column by position with multilevel column name

Time:03-03

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.

CodePudding user response:

You need specify position of columns and position of levels first:

a = a.rename(columns={a.columns[1][1]:'new_name'}, level=1)
print (a)
   a        b      
     new_name count
0  1        3     1
1  2        2     1

a = a.rename(columns={a.columns[1][0]:'new_name'}, level=0)
print (a)
   a new_name      
          sum count
0  1        3     1
1  2        2     1

CodePudding user response:

You can set the target level in rename:

a.rename(columns={'sum':'new_name'}, level=1)

For a dynamic renaming like in your answer:

a.rename(columns={a.columns[1][1]:'new_name'}, level=1)
#    a        b      
#      new_name count
# 0  1        3     1
# 1  2        2     1

a.rename(columns={a.columns[1][0]:'new_name'}, level=0)
#    a new_name      
#           sum count
# 0  1        3     1
# 1  2        2     1
  • Related