0

I wanted to make multiple sheets in excel using python pandas.df has the data which i wanted to write in excel using excel writer in python pandas. The column 'Data frame' has different values and wanted to make sheets based on the comparing the values. I wrote the code as given below :

'''

'with pd.ExcelWriter('output3.xlsx') as writer:'
        df[df["Data frame"]==0].to_excel(writer,sheet_name='zero')
        df[df["Data frame"]==1].to_excel(writer,sheet_name='one')

'''

but I am getting error message as follows:

'''

 File "<ipython-input-11-e151f874b16f>", line 3
    df[df["Data frame"]==1].to_excel(writer,sheet_name='one')
     ^
SyntaxError: invalid syntax

'''

can anyone please help me on this.

ankit5088
  • 3
  • 2

1 Answers1

0

Can you show your original code to write to excel? As for me it works, but in this question it looks like you have a indent that is not needed.

Or you can make two different dataframes first:

     df1 = df[df['Data frame']==0]
     df2 = df[df['Data frame']==1]

and than write this to two sheets with writer:

     # Create a new excel workbook
     writer = pd.ExcelWriter('output3.xlsx', engine='xlsxwriter')

     df1.to_excel(writer,sheet_name='zero')
     df2.to_excel(writer,sheet_name='one')
Renate van Kempen
  • 124
  • 1
  • 2
  • 10
  • with pd.ExcelWriter('output3.xlsx') as writer: df[df["Data frame"]==0].to_excel(writer,sheet_name='zero') df[df["Data frame"]==1].to_excel(writer,sheet_name='one') – ankit5088 Jan 24 '20 at 10:53
  • Found this answer: https://stackoverflow.com/questions/14225676/save-list-of-dataframes-to-multisheet-excel-spreadsheet Perhaps this helps you? – Renate van Kempen Jan 24 '20 at 10:55
  • the link to put different dataframes in one sheet. What i want is to put in different sheets. – ankit5088 Jan 24 '20 at 11:05
  • https://stackoverflow.com/questions/42370977/how-to-save-a-new-sheet-in-an-existing-excel-file-using-pandas And this one? – Renate van Kempen Jan 24 '20 at 11:16