Home > Software engineering >  How to save each dataframe produced by a for loop to the same excel sheet, but different sheets?
How to save each dataframe produced by a for loop to the same excel sheet, but different sheets?

Time:12-23

I'm writing python script (in jupyter notebook, using pandas and numpy) where I have a for loop that produces a single matrix each iteration, in this for loop i want each matrix to be saved to an excel file, each iteration matrix should be saved to its own sheet in the excel file before the for loop goes to the next iteration. I'd like the matrix from the 1st iteration to be saved in Sheet 1, matrix from 2nd iteration in Sheet 2,... matrix from nth iteration in Sheet n.

Below is what i have as of now, but this just rewrites each iteration matrix into the same single excel sheet. I am new to staockoverflow (and python) so not sure if i formatted my example script correctly, sorry.

> for r in range (10):
>     Matrix = np.empty((1000,317))
>     for i in range(1000):
>         for j in range (317):
>             if-else statements that fill out Matrix
> 
>     PP = pd.DataFrame(Matrix) 
>     PP.to_excel('File.xlsx', sheet_name='Sheet{}'.format(r 1), header=False, index=False)

CodePudding user response:

Please see the enter image description here

  • Related