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?

nucsit026
  • 652
  • 7
  • 16
ASH
  • 20,759
  • 19
  • 87
  • 200
  • Does this answer your question? [Renaming columns in pandas](https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas) – AMC Feb 08 '20 at 01:13

3 Answers3

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

MkWTF
  • 1,372
  • 7
  • 11
1

Try this

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

Also check this out

Pandas documentation

Manuel
  • 730
  • 7
  • 25
1

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

df = df.rename(columns={'some_column': 'new_column'}, axis=1)
alex067
  • 3,159
  • 1
  • 11
  • 17