0

Let's say that I have the following Excel file: enter image description here

I would like to use Python to create an Excel file for each sheet, with all the columns as well. So in Sheet1 to have all the lines where "Sheet1" is mentioned in column A, and so on.

I am able to create the sheets, but not to put the data into it, could someone please help? Below is my code until now:

sheets = ['Sheet1', 'Sheet10', 'Sheet2', 'Sheet3']
finalfile = 'test to check.xlsx'
writer = pd.ExcelWriter(finalfile)
for sheet in sheets:
    df.to_excel(writer,sheet)
writer.save()
2
  • Not able to understand your question. Can you please explain clearly. Commented Sep 11, 2019 at 11:50
  • I have an Excel file with 3 columns: Sheet, Name, Version. I want to create one sheet for each element on the list sheets, and the content of this sheet to be from the Excel file with the 3 columns. Basically, when in column A 'Sheet' I have 'Sheet1', I want all those lines to be in the sheet 'Sheet1'. Is it more clear? Commented Sep 11, 2019 at 11:58

1 Answer 1

1

I suppose the below correction in your code should work fine for your expected output.

for sheet in sheets:
    df.loc[(df['Sheet'] == sheet)].to_excel(writer, sheet)

Let me know if this works :)

Sign up to request clarification or add additional context in comments.

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.