1

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?

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.