0

I Am running the same pandas script on 100+ subjects, but I need to put all of them into the same sheet vertically.

Both of these are labeled as "score" in my script, as it loops through the script once for the first score, and then produces the 2nd DF for the 2nd score. I need it to save the first score in the excel sheet, and then append the 2nd and many more scores below it

here is score 1:

labelCTBIN001v1   Overall Stress Score      Stress Score  Short-Term Stress Risk  Future Stress Risk  Chronic Stress Risk
scoreCTBIN001v1                     57                49                      54                  66                   60

and here is score 2:

labelCTBIN003v1   Overall Stress Score       Stress Score  Short-Term Stress Risk  Future Stress Risk  Chronic Stress Risk
scoreCTBIN003v1                     57                 52                      52                  65                   59

I'm just looking for something like this in an excel sheet:

labelCTBIN001v1   Overall Stress Score      Stress Score  Short-Term Stress Risk  Future Stress Risk  Chronic Stress Risk
scoreCTBIN001v1                     57                49                      54                  66                   60
labelCTBIN003v1   Overall Stress Score       Stress Score  Short-Term Stress Risk  Future Stress Risk  Chronic Stress Risk
scoreCTBIN003v1                     57                 52                      52                  65                   59

I've tried doing the following to write to excel sheets for pandas,

with pd.ExcelWriter('Scores.xlsx',mode='a',if_sheet_exists='overlay') as writer:  
    (score.transpose()).to_excel(writer, sheet_name='Scores_new')

in the docs it stats "mode='a'" is used to append to a sheet, however it is always overwritten with the latest subjects data.

  • Maybe you could use pandas concat to append and then save it to excel – Nohman Jul 20 '22 at 21:18
  • @Nohman concat will not work as these scores are generated independently of eachother, so there is only ever one DF in the "score" variable at a time, as the script runs once for each subject. – Michael Schmitz Jul 20 '22 at 21:21
  • Check this thread https://stackoverflow.com/questions/20219254/how-to-write-to-an-existing-excel-file-without-overwriting-data-using-pandas – Nohman Jul 20 '22 at 21:26

0 Answers0