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
  • 622
  • 5
  • 15
ASH
  • 18,040
  • 13
  • 61
  • 153
  • 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,292
  • 7
  • 10
1

Try this

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

Also check this out

Pandas documentation

Manuel
  • 630
  • 4
  • 22
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
  • 2,758
  • 1
  • 9
  • 16