Multiple pandas.dataframe to one csv file

后端 未结 2 1320
庸人自扰
庸人自扰 2020-12-29 17:16

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,

相关标签:
2条回答
  • 2020-12-29 17:40

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

    for df in list_of_dfs:
        with open('all_dfs.csv','a') as f:
            df.to_csv(f)
            f.write("\n")
    
    0 讨论(0)
  • 2020-12-29 17:41

    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.

    0 讨论(0)
提交回复
热议问题