Pandas writing dataframe to CSV file

2019-01-03 00:45发布

I have a dataframe in pandas which I would like to write to a CSV file. I am doing this using:

df.to_csv('out.csv')

And getting the error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)

Is there any way to get around this easily (i.e. I have unicode characters in my data frame)? And is there a way to write to a tab delimited file instead of a CSV using e.g. a 'to-tab' method (that I dont think exists)?

7条回答
The star\"
2楼-- · 2019-01-03 01:17

it could be not the answer for this case, but as I had the same error-message with .to_csv I tried .toCSV('name.csv') and the error-message was different ("'SparseDataFrame' object has no attribute 'toCSV'"). So the problem was solved by turning dataframe to dense dataframe

df.to_dense().to_csv("submission.csv", index = False, sep=',', encoding='utf-8')
查看更多
登录 后发表回答