多个pandas.dataframe到一个csv文件 [英] Multiple pandas.dataframe to one csv file
本文介绍了多个pandas.dataframe到一个csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有多个熊猫数据框,希望将它们写为一个CSV文件.最直接的方法是什么?
I have multiple pandas dataframes, and hope to write them as one CSV file. What is the most straightforward way?
例如,从以下四个数据帧中,
For example, from following four dataframes,
如何在CSV以下创建?
how can I create below CSV?
注意:所有数据框都具有相同的尺寸.
Note The dataframes all have the same dimensions.
推荐答案
一种非常简单的方法是 to_csv
:
A very straightforward way would be to concat
pairs horizontally, concat the results vertically, and write it all out using to_csv
:
import pandas as pd
pd.concat([
pd.concat([df1, df2], axis=1),
pd.concat([df3, df4], axis=1)]).to_csv('foo.csv')
可能更节省内存的方式是将其逐个编写:
A possibly more memory-conserving way would be to write it piecemeal:
with open('foo.csv', 'w') as f:
pd.concat([df1, df2], axis=1).to_csv(f)
with open('foo.csv', 'a') as f:
pd.concat([df3, df4], axis=1).to_csv(f, header=False)
省略headers=False
将重复标题.
这篇关于多个pandas.dataframe到一个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文