【问题标题】:Trouble in renaming column in a pandas DataFrame Python 3.7在 Pandas DataFrame Python 3.7 中重命名列的问题
【发布时间】:2020-06-06 01:27:44
【问题描述】:

我有以下列并希望将“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”没有改变。 这里有什么问题。

【问题讨论】:

  • 'Store_num' 不是'Stores_num'
  • 哦,废话……我的眼睛出卖了我……

标签: python-3.x pandas dataframe rename


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 2021-04-24
    • 2017-03-05
    • 1970-01-01
    • 2019-12-01
    • 2021-05-23
    • 2021-08-21
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多