Error when writing python pandas dataframe to csv file
Asked Answered
N

1

9

I have a problem writing a Pandas dataframe to a csv file. I guess there are som characters that can not be translated but I do not know how to fix the problem. I need help on this.

Here is my simple call and the error message:

big_frame.to_csv('C:\DRO\test.csv')

error:

C:\Python27\lib\site-packages\pandas\lib.pyd in pandas.lib.write_csv_rows (pandas\lib.c:13528)()

UnicodeEncodeError: 'ascii' codec can't encode character u'\xd6' in position 1: ordinal not in range(128)
Nickens answered 3/1, 2014 at 16:56 Comment(0)
O
14

Try using a different file encoding: big_frame.to_csv('C:\DRO\test.csv', encoding='utf-8')

Overpower answered 3/1, 2014 at 17:6 Comment(1)
It just lets python know what kind of data it's writing (e.g., ascii, unicode). As the error message says, ascii (the default) can't deal with unicode characters (\xd6)Overpower

© 2022 - 2024 — McMap. All rights reserved.