Can I save it on several sheets at once when I save it as Excel in Pandas?

Asked 2 years ago, Updated 2 years ago, 49 views

When Panda stores data frames in Excel, Can I make several sheets at once? df.to_excel() is made of just one sheet The first data frame is located on the first sheet called "a" How can I make sure that the second data frame is stored in the second sheet called "b"?

Is there only a way to load and add files already saved by ExcelWriter?

python pandas

2022-09-20 22:11

1 Answers

I googled it with pandas to_excel multiple sheets.

src : https://xlsxwriter.readthedocs.io/example_pandas_multiple.html

#
# # An example of writing multiple dataframes to worksheets using Pandas and
# # XlsxWriter.
#
# # Copyright 2013-2020, John McNamara, [email protected]
#

import pandas as pd


# # Create some Pandas dataframes from some data.
df1 = pd.DataFrame({'Data': [11, 12, 13, 14]})
df2 = pd.DataFrame({'Data': [21, 22, 23, 24]})
df3 = pd.DataFrame({'Data': [31, 32, 33, 34]})

# # Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_multiple.xlsx', engine='xlsxwriter')

# # Write each dataframe to a different worksheet.
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
df3.to_excel(writer, sheet_name='Sheet3')

# # Close the Pandas Excel writer and output the Excel file.
writer.save()


2022-09-20 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.