没有我 运行 重命名此列的代码将起作用

No code I run to rename this column will work

我正在尝试重命名此 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

但是,我尝试的任何代码都不起作用,列名称保持不变。到目前为止我有 运行:

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)

将列名放入列表中得到:

print(df_cig_sales.columns.tolist())

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

有什么我想念的吗?此代码 运行ning 没有任何错误。任何帮助都是极好的。谢谢。

如果您删除尾随空格,这应该有效:

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