Home > Blockchain >  No code I run to rename this column will work
No code I run to rename this column will work

Time:05-12

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:

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.

CodePudding user response:

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 ---^
  • Related