10

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,

enter image description here

how can I create below CSV?

enter image description here

Note The dataframes all have the same dimensions.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
Light Yagmi
  • 5,085
  • 12
  • 43
  • 64

2 Answers2

15

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)

Omitting headers=False would repeat the headers.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
10

For anyone that needs to append multiple dataframes to same csv vertically, you can use this:

with open('all_dfs.csv','a') as f:
    for df in list_of_dfs:
        df.to_csv(f)
        f.write("\n")
Community
  • 1
  • 1
Nicolae Stroncea
  • 587
  • 2
  • 6
  • 17