I am trying to save a pandas dataframe as an excel sheet. The dataframe as names with Ö, Ä, Å, Ø, Æ due to which i get the error:
'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)
I am using the following code of lines:
import sys
reload(sys)
sys.setdefaultencoding('utf8')
writer = pd.ExcelWriter('/filepath/filename.xls')
df.to_excel(writer,'Sheet1',index=False)
writer.save()
I tried several solutions as specified by the following but no success:
How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"
How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"
Example code:
I have the following code as an example dataframe:
d = {'col1': ['Äse', 'SÖA'], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
Now if try to save it using the following lines of code, I get the ascii, unicode error.
import sys
reload(sys)
sys.setdefaultencoding('utf8')
writer = pd.ExcelWriter('/filepath/filename.xls')
df.to_excel(writer,'Sheet1',index=False)
writer.save()
I tried adding the "encoding='utf8'" parameter to to_excel as well as removing the first three lines but it did not work.
Your help is highly appreciated, thanks!
Can anybody help in this matter?