Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Appending multiple dataframes to excel on different sheets

I would like to add multiple dataframes from a dictionary to one excel file and each dataframe should be written in a sheet. Sheen name is actually the key of the dictionary. The following code

for key in df_dict:
    df = df_dict[key]
    df.to_excel('data-frames.xlsx', sheet_name=key, index=False)

only writes the last dataframe. So, I only see one sheet. On the other hand the append_df_to_excel() method which is described here doesn’t works. I get the error that there is no such method, although the pandas version is 1.4.2.

...
AttributeError: 'DataFrame' object has no attribute 'append_df_to_excel'

In [2]: import pandas as pd
In [2]: print(pd.__version__)
1.4.2

How can I fix that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Could you try this code?

for key in df_dict:
    df = df_dict[key].copy()
    with pd.ExcelWriter('data-frames.xlsx', mode='a') as writer:  
           df.to_excel(writer, sheet_name=key)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading