Reputation: 473
I have a Python script which uses some embedded SQL to create a series of dataframes. Each dataframe holds a list of ID numbers, like this:
ID_number
---------
123
456
789
What I want to do is create a csv or xlsx file in which the results of each dataframe are held in their own tab, along with a brief header string in cell A1. So for example, the first tab would look like this:
The results from dataframe one are:
123
456
789
I know I can write the results of a dataframe to a csv or xlsx file using the to_csv or to_excel functions, a bit like this:
df.to_csv("my_file.csv", mode ='x', index = False)
However, I'm not sure how to write multiple dataframes to different tabs within the same file, or add the header string.
Does anyone know how to do this please?
Upvotes: 1
Views: 188
Reputation: 1986
pandas does export multiple df in one excel. try this...
Excelwriter = pd.ExcelWriter(r"C:\Users\exportdf.xlsx",engine="xlsxwriter")
df1.to_excel(Excelwriter, sheet_name="first_tab", index=False)
df2.to_excel(Excelwriter, sheet_name="second_tab", index=False)
Excelwriter.save()
Hope this Helps...
Upvotes: 1