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

How can I make separate excel sheets (in 1 excel file) for each Name on my pandas dataframe?

So I’ve been wondering how can I print my pandas dataframe for each Name on the dataframe into each of their own worksheet inside one excel file. For example, this is my pandas dataframe:

data = {'Name':['Employee1','Employee1','Employee1','Employee2','Employee2','Employee2','Employee3','Employee3','Employee3'],
                    'Date (July)':[22,23,24,22,23,24,22,23,24], 'Working_Hours': [7,7,8,8,8,8,8,7,9], 'Overtime':[0,0,1,1,1,1,1,0,2]}
    dataframe = pd.DataFrame(data)
    dataframe

enter image description here
And this is how the excel file I’m trying to create looks like (each Name has their own worksheet where their data is printed within):

enter image description here

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

Thank you in advance! I’d really appreciate your help ^^

>Solution :

The following should work:

import pandas as pd

df = pd.DataFrame({'Name': ['John', 'Jane', 'Joe', 'Jack']})

with pd.ExcelWriter('Sheet.xlsx') as writer:
    for name in df['Name']:
        df[df['Name'] == name].to_excel(writer, sheet_name=name, index=False)

You can replace your DataFrame in its place but the main part of writing to different sheets should work!

Hope it helps!

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