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

Question:

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

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

Asked By: ioalft

||

Answers:

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!

Answered By: Altaf Shaikh