2

I have a big dataset (df) (8M rows, 50 columns). I am trying to build a for loop to create an excel file where each sheet holds the value_counts() of each of the column of the dataset.

(i.e. on worksheet('Sheet1') I write df.columns[0].value_counts() and on worksheet('Sheet2') I write df.columns[1].value_counts() etc etc).

Here's what I tried:

for i in range(3,6):   # I am using a small range to test the loop
    z = df1[df1.columns[i]].value_counts()
    z = z.to_frame().reset_index()
    title = str(i)
    with pd.ExcelWriter('Pivot part1.xlsx') as writer:  
        z.to_excel(writer, sheet_name=title)

This keeps overwrite the file so that I ended up with an excel file with only one sheet rather the an Excel file with 4 sheets.

I hope I managed to explain clearly the issue and I apologize if this question is a duplicate, but I couldn't find a suitable answer, or at least one I could understand.

2 Answers 2

2

Re-arrange so that you only open the excel writer object once:

with pd.ExcelWriter('Pivot part1.xlsx') as writer:  
    for i in range(3,6):
        z = df1[df1.columns[i]].value_counts()
        z = z.to_frame().reset_index()
        title = str(i)
        z.to_excel(writer, sheet_name=title)
Sign up to request clarification or add additional context in comments.

Comments

2

You create the writer object once and it shall all work fine.

writer = pd.ExcelWriter('Pivot part1.xlsx', engine='xlsxwriter')
for i in range(3,6):   # I am using a small range to test the loop
    z = df1[df1.columns[i]].value_counts()
    z = z.to_frame().reset_index()
    title = str(i)
    z.to_excel(writer, sheet_name=title)
writer.save()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.