Pandas将数据帧写入CSV文件 [英] Pandas writing dataframe to CSV file

查看:903
本文介绍了Pandas将数据帧写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大熊猫的数据框,我想写一个CSV文件。我这样做使用:

  df.to_csv('out.csv')



得到错误:

  UnicodeEncodeError: 'ascii'编解码器不能编码字符u'\\\α'在位置20:序数不在范围内(128)

有没有办法容易解决这个问题(即我的数据框中有unicode字符)?并且有一种方法写入一个制表符分隔文件,而不是CSV使用例如。

解决方案

要使用选项卡进行定界,您可以使用 sep <$ c的参数$ c> to_csv

  df.to_csv(file_name,sep ='\\ \\ t')

要使用特定编码(例如'utf-8'), c $ c> encoding 参数:

  df.to_csv(file_name,sep ='\t ',encoding ='utf-8')


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)?

解决方案

To delimit by a tab you can use the sep argument of to_csv:

df.to_csv(file_name, sep='\t')

To use a specific encoding (e.g. 'utf-8') use the encoding argument:

df.to_csv(file_name, sep='\t', encoding='utf-8')

这篇关于Pandas将数据帧写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆