I have a dataframe that I want to export to Excel. I'm new to python and pandas so I need some help on this simple task.
df2.to_excel('C:\BT\stack_test3.xlsx')
Error message:
IOError: [Errno 13] Permission denied: 'C:\BT\stack_test3.xlsx'
You path is incorrect, because you have not escaped the slashes it thinks you are trying to write to the root of c: drive use the following:
df2.to_excel(r'C:\BT\stack_test3.xlsx')
The r makes the path a raw string and means you do not need to escape the slashes
Edit
It seems that there is some error with openpyxl as using
df2.to_excel(r'C:\BT\stack_test3.xls')
works which uses xlwt, I don't know enough about those packages so it could be either a permissions problem with openpyxl which I have not been able to find anything about or a bug.
s = 'A\B\C' print ord("\\") for ch in s: print ord(ch) you will see the backslashes are in the string. So the backslash should only create a problem if \B or \s is a recognized escape sequence, and neither is a recognized escape sequence. The op should use raw strings regardless to avoid any potential problems, or use forward slashes, but I don't see how this particular path is problematic.print len('A\B\C'). Compare to print len('C:\b\a')to_csv' without the r` prefix and got a [Errno 22] invalid mode ('w') or filename error, adding the r prefix fixes it, incidentally the expanded path without r prefix did not expand the middle slash so c:\\BT\stack_test3.xlsx would be displayed, if adding r prefix does not solve your problem then I will delete my answer"C:\BT\stack_test3.xlsx" is interpreted by windows as C:BTstack_test3.xlsx
df2.to_excel(r'C:\BT\stack_test3.xlsx')? As I think your path is wrong and python thinks you are writing toc:as your back slash has not been escaped .rprefix fix your problem?.xls, .xlsx uses openpyxl , and.xlsextension uses xlwt, would be interesting to see if this is a problem with both libraries.