I have a pandas dataframe and am trying to export this to excel file using pandas to_excel function.
I have written the code as below :
writer = pd.ExcelWriter(output_file_path, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False, encoding='utf-8')
writer.save()
Error :
UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position 23: ordinal
not in range(128)
I have tried the below options and didn't get output.
1)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
2)
def changeencode(data):
cols = data.columns
for col in cols:
if data[col].dtype == 'O':
data[col] = data[col].str.decode('utf-8').str.encode('ascii', 'ignore')
return data
Does anyone know any other solutions ?