Reputation: 67
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()
Upvotes: 0
Views: 333
Reputation: 490
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 :)
Upvotes: 1