Predictive Hacks

How to Write multiple pandas Data Frames to Excel tabs

Assume that you have many pandas data frames and you want to save them to a single excel file of many worksheets (tabs). Let’s see how we can do it:

# create the xlswriter and give a name to the final excel 
# for example Final.xlsx
writer = pd.ExcelWriter('Final.xlsx', engine='xlsxwriter')


# it is convenient to store the pandas dataframes in a  
# dictionary, where the key is the worksheet name that you want to give 
# and the value is the data frame
df_dict = {'My_First_Tab': df1, 'My_Second_Tab': df2,
        'My_Third_Tab':df3, 'My_Forth_Tab':df4}

#iterate over the data frame of dictionaries
for my_sheet, dframe in  df_dict.items(): 
    dframe.to_excel(writer, sheet_name = my_sheet, index=False)

# finaly you have to save the writer
# and the Final.xlsx has been created
writer.save()

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s