How can I export all dataframes into an Excel file

Question:

I have a notebook open with about 45 dataframes. I would like to export all of them into a single Excel file with each dataframe being it’s own tab in Excel.

Is there an easy way to do this without having to write each tab out manually?

Thank you!

Asked By: CIHAnalytics

||

Answers:

Please check the link Example: Pandas Excel with multiple dataframes

You can then as suggested by @delimiter create a list of the names

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')

list = [df1,df2,df3]
names = ['df1','df2','df3']
for i in range(len(list)):
    list[i].to_excel(writer, sheet_name=names[i])
Answered By: Ali Sultan

You can do this:

first create a list of all the dataframes that you need to write.

l=[df1,df2,df3...]

writer=pd.ExcelWriter('multi_df.xlsx',engine='xlsxwriter')

for i,df in enumerate(l):
    df.to_excel(writer,sheet_name="df_"+str(i+1))
Answered By: Suhas Mucherla
  1. Create a pandas excel writer instance and name the excel file
    xlwriter = pd.ExcelWriter('Customer_Details.xlsx')

If you don’t include a file path like ‘C:UsersRonDesktopFile_Name.xlsx’, it will save to your default folder, that is where the file you’re reading from is located.

#2. Write each dataframe to a worksheet with a name

    dfName.to_excel(xlwriter, sheet_name = 'Name', index = False)
    dfAddress.to_excel(xlwriter, sheet_name = 'Address', index = False)
    dfContact.to_excel(xlwriter, sheet_name = 'Contact', index = False)

#3. Close the instance

xlwriter.close()

source youtu.be

Answered By: Ivan Posazhennikov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.