将pandas DataFrame写入CSV文件 [英] Writing a pandas DataFrame to CSV file
本文介绍了将pandas DataFrame写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据框,我想将其写入CSV文件.我正在使用以下方法进行操作:
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')
并收到错误消息:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)
有什么方法可以轻松解决此问题(即,我的数据框中有Unicode字符)吗?并且有一种方法可以写入制表符分隔的文件,而不是使用例如CSV格式的CSV文件.一种制表"方法(我认为不存在)?
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)?
推荐答案
To delimit by a tab you can use the sep
argument of to_csv
:
df.to_csv(file_name, sep='\t')
要使用特定的编码(例如'utf-8'),请使用encoding
参数:
To use a specific encoding (e.g. 'utf-8') use the encoding
argument:
df.to_csv(file_name, sep='\t', encoding='utf-8')
这篇关于将pandas DataFrame写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文