2

I would like to rename one of my levels in a multiindexed columns dataframe in pandas.

df.columns.names

gives me

FrozenList(['level0', 'level1'])

I want to rename 'level0' to 'main'.

I have tried different approaches, none works:

 df.columns.set_names('findingkey', level=0, inplace=True)

gives me TypeError: 'list' object is not callable

I also tried to do it directly:

df.columns.names[0]='main'

with output: TypeError: 'FrozenList' does not support mutable operations.

0

1 Answer 1

3

Use:

df.columns.names = ['main', 'level1']

Or

df = df.rename_axis(['main', 'level1'], axis=1)
Sign up to request clarification or add additional context in comments.

5 Comments

First one works, I am just puzzled that df.columns.names[0]='main' did not work then...
Hmm I think error is clear - 'FrozenList' does not support mutable operations, so only assign all new values works.
and there is no method that allows to rename only one?
Unfortunately not :(
only df.columns.names = ['main'] + [df.columns.names[1]] - but it is a bit crazy ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.