2

I can't seem to rename my columns in a dataframe.

I tried this: df2.columns['Rating','Spread','Cnt']

When I look at the dataframe, the Cnt is not there.

I also tried this: df2.rename(columns={'Rating','Spread','Cnt'}, inplace=True)

Again, after I run the script, the Cnt doesn't show up. All I have is the first two field names; the third one keeps dropping off. How can I fix this?

1

3 Answers 3

3

You need to pass a dictionary to the columns argument in the rename function, like this:

df.rename(columns={"old_name": "new_name"}) 

Check the documentation

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

df2.rename(columns={'OldName1':'Rating','OldName2':'Spread','OldName3':'Cnt'}, inplace=True) 

Also check this out

Pandas documentation

Comments

1

You have to tell pandas what you want to change the name to.

df = df.rename(columns={'some_column': 'new_column'}, axis=1) 

1 Comment

FYI, axis=1 is redundant in this case since you have passed columns as an argument

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.