What to do about the error you faced when generating the file with the pandas to_excel method.
You can output two or more sheets in one file, Use "Excel Writer" to add a sheet to an existing file.
At this time, the above error occurred in the path setting.
I was addicted to it, so make a note of the cause and solution.
-The "~" that points to the home directory cannot be used. └ Described in "C: / Users /" ・ When using a backslash (or yen mark), describe it with "//" (\) └ Escape with one └ "/" can be used.
It seems that "~" can be used with to_excel and read_excel, but it cannot be used with Excel Writer.
It can be used when specifying the path with the read_excel method, but it cannot be used with Excel Writer.
code
df2 = df.copy()
with pd.ExcelWriter('~/Desktop/GA-demo.xlsx') as writer:
df.to_excel(writer, sheet_name='AAA')
df2.to_excel(writer, sheet_name='BBB')
error
FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/GA-demo.xlsx'
code
df2 = df.copy()
with pd.ExcelWriter('~\\Desktop\\GA-demo.xlsx') as writer:
df.to_excel(writer, sheet_name='AAA')
df2.to_excel(writer, sheet_name='BBB')
error
FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/GA-demo.xlsx'
OK
df2 = df.copy()
with pd.ExcelWriter('C:/Users/name/Desktop/GA-demo3.xlsx') as writer:
df.to_excel(writer, sheet_name='AAA')
df2.to_excel(writer, sheet_name='BBB')
I get this error when I copy and paste the folder path.
** ▼ Error ** For example, if you specify the path as follows
code
df2 = df.copy()
with pd.ExcelWriter('C:\Users\name\Desktop\GA-demo.xlsx') as writer:
df.to_excel(writer, sheet_name='AAA')
df2.to_excel(writer, sheet_name='BBB')
error
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
** Backslash "" is for escaping. If you want to recognize it as a character, you need to use "\". ** **
OK!
df2 = df.copy()
with pd.ExcelWriter('C:\\Users\\name\\Desktop\\GA-demo2.xlsx') as writer:
df.to_excel(writer, sheet_name='AAA')
df2.to_excel(writer, sheet_name='BBB')
The description can be either of the following. 「C:\Users\name\」 「C://Users//name//」
Recommended Posts