There are plenty of similar questions. Here are two:
Python Error: AttributeError: 'NoneType' object has no attribute 'to_excel'
AttributeError: 'Object has no attribute'
I want to replace underscores with a space in the excel file column header and then save it. Here is the code:
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)
Here's the full error:
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'
While debugging, I notice the script will print the columns.str.replace()
function successfully. But, it won't write to the excel file.