pandas DataFrame Python 3.7中重命名列的问题

问题描述 投票:0回答:1

我有以下几栏,希望将'Stores_num'更改为'Stores_Num'

data.columns
Out[240]: 
Index(['INV_NUMBER', 'Store_num', 'Description', 'Price', 'Sold', 'Del',
       'Sales', 'Tot_Sls', 'Unit_Cost', 'Cost', 'Cost_Percent', 'Margin',
       'Profit', 'Date', 'Year', 'Month', 'Day'],
      dtype='object')

我使用以下代码:

data.rename(columns={'Stores_num':'Stores_Num'},inplace=True)

data.columns
Out[242]: 
Index(['INV_NUMBER', 'Store_num', 'Description', 'Price', 'Sold', 'Del',
       'Sales', 'Tot_Sls', 'Unit_Cost', 'Cost', 'Cost_Percent', 'Margin',
       'Profit', 'Date', 'Year', 'Month', 'Day'],
      dtype='object')

如您所见,列名“ Stores_num”未更改。这是怎么了。

python-3.x pandas dataframe rename
1个回答
0
投票

您的代码没有错。看起来您正在使用Jupyter Notebook运行代码。以前的数据有可能存储在缓存中,请重新启动并运行所有单元。它应该解决问题。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.