0

I'm importing a CSV file which has the following header(column names):

"Appendix","Name / Organization","Issuer","Algorithm"

I tried changing the "Appendix" column name into "Other Info" but it doesn't work.

df.rename(columns={'Appendix':'Other Info'}, 
                 inplace=True)

I get no error and when I print the dataframe again, it looks like the original one. (nothing has changed). I don't understand why. Can you give me an idea?

Thank you!

zimskiz
  • 127
  • 1
  • 9
  • can you try `df.columns = df.rename(columns={'Appendix':'Other Info'})` ditch the inplace, it's best not to use it. – Umar.H Oct 05 '20 at 09:49
  • Check the output of `df.columns`. There could be `whitespaces` in your column headers. You would need to handle those in rename command as well. – Mayank Porwal Oct 05 '20 at 09:51
  • Index(['"Appendix","Name / Organization","Issuer","Algorithm"'], dtype='object'). Therefore, no whitespaces... – zimskiz Oct 05 '20 at 10:07
  • i see a additional quotation mark `'"Appendix"` – Umar.H Oct 05 '20 at 10:48

2 Answers2

1

Some methods to sort this out:

  1. Restart the kernel and run again.

  2. If it doesn't help, assign a list of new column names

    df.columns = ['Other Info' , 'Name / Organization' , 'Issuer' , 'Algorithm']

  3. Create a new column Other Info, copy all the data from Appendix and drop your Appendix column

Noob Geek
  • 409
  • 6
  • 20
1

make sure column name doesn't have any invisible characters line space and'\n'. The key u give in df.replace function should match the column name.

try theese

    df.rename(columns={'df.columns[0]':'Other Info'}, 
             inplace=True)

or

    df.columns=["Other info","Name / Organization","Issuer","Algorithm"]
gilf0yle
  • 1,092
  • 3
  • 9
  • '\n' can appear in groupby operations of Pandas for the new column names. It is better to do an assignation after aggregation operations. – NuValue Apr 20 '22 at 19:28