【发布时间】:2019-10-24 23:51:24
【问题描述】:
嗨,我有一个列名以“2018”结尾的数据框
我需要从这些列名中删除年份,但遇到了一些麻烦。我还需要从这些列名中去除前导和尾随空格。
我已经尝试过以下方法:
df.columns.str.replace('\d+',"") #to try and remove the numbers from the column names
df.columns = df.columns.str.strip('') #to try and get rid of the spaces
这些对数据框没有任何作用。
我希望列名从“Stock 2018”变为“Stock”
但这并没有发生。感谢您的帮助!
【问题讨论】:
标签: python pandas iteration renaming