1

I have a dictionary of dataframes and need to export them to excel.

mycollection = {'company1': df1, 'company2': df2, 'company3': df3}

I need to export all 3 dataframes to a single excel file using 3 sheets. Each sheet should be named the company number for e.g. "company3".

There are around 200 dataframes in the mycollection dictionary. I can use to_excel method to export data to excel, but that will create 200 excel files. Is it possible to export dataframe to sheets?

1 Answer 1

1

Use ExcelWriter with iter dicts and write by DataFrame.to_excel:

with pd.ExcelWriter('output.xlsx') as writer:  
    for k, v in mycollection.items():
        v.to_excel(writer, sheet_name=k)
Sign up to request clarification or add additional context in comments.

2 Comments

# InvalidWorksheetName: Excel worksheet name 'XXXX_YYYY_promo_194_del_ZZZ_may2017' must be <= 31 chars.
It seems that I need to truncate long sheet names to 31 characters without duplicating! sheet_name=k[:30] may or may not always work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.