将 Pandas DataFrame 写入 CSV 文件 [英] Writing a pandas DataFrame to CSV file
本文介绍了将 Pandas DataFrame 写入 CSV 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Pandas 中有一个数据框,我想将其写入 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 使用例如'to-tab' 方法(我认为不存在)?
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_csv
:
To delimit by a tab you can use the sep
argument of to_csv
:
df.to_csv(file_name, sep=' ')
要使用特定的编码(例如utf-8"),请使用 encoding
参数:
To use a specific encoding (e.g. 'utf-8') use the encoding
argument:
df.to_csv(file_name, sep=' ', encoding='utf-8')
这篇关于将 Pandas DataFrame 写入 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文