Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No code I run to rename this column will work

I am trying to rename the last column in this df:

    Entity  Year    Sales of cigarettes per adult per day (International Smoking Statistics(2017))
0   Armenia 1988    12.0
1   Armenia 1989    11.6
2   Armenia 1990    11.9
3   Armenia 1991    9.3
4   Armenia 1992    4.9

However, no code that I try will work, the column name just stays the same. So far I have run:

df_cig_sales.rename({'''Sales of cigarettes per adult per day (International Smoking Statistics (2017))''':'Cigs per adult per day'},
 axis=1, inplace=True)

df_cig_sales.rename({'Sales of cigarettes per adult per day (International Smoking Statistics (2017))':'Cigs per adult per day'},
 axis=1, inplace=True)

df_cig_sales.rename(columns={'Sales of cigarettes per adult per day (International Smoking Statistics (2017))':'Cigs per adult per day'},
 inplace=True)

Putting the column names to a list gives me:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(df_cig_sales.columns.tolist())

>>>>['Entity', 'Year', 'Sales of cigarettes per adult per day (International Smoking Statistics (2017)) ']

Is there something that I am missing? This code is running without any errors. Any help would be awesome. Thank you.

>Solution :

This should works if you remove the trailing whitespace:

s = 'Sales of cigarettes per adult per day (International Smoking Statistics(2017)) '
df_cig_sales.rename(columns={s: 'Cigs per adult per day'}, inplace=True)
>>> s
'Sales of cigarettes per adult per day (International Smoking Statistics (2017)) '
#                                                   Trailing whitespace here ---^
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading