【发布时间】:2021-01-29 20:26:28
【问题描述】:
有很多类似的问题。这里有两个:
Python Error: AttributeError: 'NoneType' object has no attribute 'to_excel'
AttributeError: 'Object has no attribute'
我想将excel文件列标题中的下划线替换为空格,然后保存。代码如下:
import pandas as pd
ws = r'c:/users/jpilbeam/Reverse911_1a_English_.xlsx'
# data frame from excel file
df3 = pd.read_excel(ws, header=0)
#remove underscores
df2 = df3.columns.str.replace("_", " ")
## save to file
df2.to_excel(df2)
这是完整的错误:
Traceback (most recent call last):
File "\\pathto\Python Scripts\C19VaccinationTable.py", line 18, in <module>
df2.to_excel(df2)
AttributeError: 'Index' object has no attribute 'to_excel'
在调试时,我注意到脚本将成功打印columns.str.replace() 函数。但是,它不会写入 excel 文件。
【问题讨论】: