pandas的DataFrame输出结束csv [英] pandas DataFrame output end of csv

查看:186
本文介绍了pandas的DataFrame输出结束csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将新的DataFrame数据添加到现有csv文件的末尾? to_csv没有提到这样的功能。先谢谢你。

解决方案

您可以使用 to_csv .python.org / 2 / library / functions.html#openrel =noreferrer>以追加模式开启

  with open(file_name,'a')as f:
df.to_csv(f,header = False)

使用 header = None ,以便不附加列名称。

事实上,pandas有一个包装器在 to_csv 中使用模式请参阅Joe的回答):

  df.to_csv(f,mode ='a',header = False)


I wonder how to add new DataFrame data onto the end of an existing csv file? The to_csv doesn't mention such functionality. Thank you in advance.

解决方案

You can append using to_csv by passing a file which is open in append mode:

with open(file_name, 'a') as f:
    df.to_csv(f, header=False)

Use header=None, so as not to append the column names.

In fact, pandas has a wrapper to do this in to_csv using the mode argument (see Joe's answer):

df.to_csv(f, mode='a', header=False)

这篇关于pandas的DataFrame输出结束csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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