Home > OS >  Export multiple pandas dataframes in one csv
Export multiple pandas dataframes in one csv

Time:11-15

I'm trying to export two separate pandas dataframes into one csv file. To merge the dataframes is not an option because they describe different things.

If possible I would also like to display one (or more) sentences between the tables.

Here as an example: [Dataframe X] [Dataframe Y]

And this is the result I would like to somehow get to: [Excel view]

It seems that pandas to_csv is not offering any solution to my problem, am I correct? So if anyone has some kind of help or thoughts about this issue, your help is really appreciated.

Thank you in advance and warm greetings

CodePudding user response:

A great thing to look at would be XLWings: https://www.xlwings.org/

Although XLWings does not work with saving as ".csv" format, creating the Excel file is pretty simple here and then you can just save the Excel workbook as CSV in Excel.

You can then open a workbook and sheet and assign different cells to specific dataframes, for example if an excel file exists:

import xlwings as xl

wb = xl.Book(path) #open the workbook of the path
sheet = xl.sheets['Sheet1'] #open specified sheet
sheet.range('A1').options(index=True).value = df1 #specify which cell to start and assign dataframe
sheet.range('F1').options(index=True).value = df2
wb.save(path) #save and close
wb.close()

You can assign if you want to include the index within the options parameter.

Edit: There has been examples where people have got XLWings to go to CSV if it has to be in CSV format : https://stackoverflow.com/a/68824974/20356301.

  • Related