1

Recently I have posted a problem regarding writing a spilted dataframe into different excel sheets this post, and I somehow find an answer which resulted in writing each of them in a separate excel file rather than separate excel sheets. I have also read the recommended post here but that also was not much help. I am wondering if I could find a solution to my question and I really feel that it is not that much complicated but since I am new in this field, I could not find it.

mpy
  • 622
  • 9
  • 23

1 Answers1

1

Combining solutions from both the links that you posted, here is the solution

# define an excel writer first 
writer = pd.ExcelWriter("output.xlsx", engine = 'xlsxwriter')

df_split = np.array_split(promotion1, 4)
for index, df_sub in enumerate(df_split):
   #print(df_sub.head())
   # save each of your splitted dataframes using excel writer
   df_sub.to_excel(writer, sheet_name = 'sheet' + str(index))

MNA
  • 183
  • 2
  • 8
  • problem solved:) thanks. however since I am new to python and coding, I am wondering if you could say the difference between using simply df.to_excel() and creating a writer object and using an engine? – mpy Sep 12 '19 at 06:12
  • 1
    df.to_excel will create new excel file every time you execute it. However, by using writer object you can add new sheets to existing excel file – MNA Sep 12 '19 at 07:47