Pandas to_csv only saves the data from the last run of my script. How can I create a large table to save all my simulations?

Viewed 500

I am fairly new to python. I am currently trying to do multiple runs of my simulation so I can see how my data differs from each run. I am currently running this code, from my Model class, to create my file and data frame, including the dictionary. The epochs are just the days in each simulation:

def setup_model(num_epochs):

     model = Model(epochs=num_epochs)

     model.output["student_stats"] = {
        "healthy": [],
        "infectious": [],
        "absent": [],
        "recovered": []
     }
     student_stats = model.output["student_stats"]

Here is the code where I set up the pd data frame:

df = pd.DataFrame(student_stats)
df.to_csv("file_name.csv", header=True)

I launch my simulation from my Launcher.py and I import the setup_model method. Below is code from my Launcher.py:

from Model import setup_model

for i in range(5):

    setup_model(30)

When I run my simulation though I only get the output from one run (each simulation is 30 days long), I wish to get output from 5 runs of the simulation. Here is an example of my current output:

,healthy,infectious,absent,recovered
0,125,1,0,0
1,124,2,0,0
2,123,2,1,0
3,122,2,2,0
4,121,2,3,0
5,120,2,3,1
6,119,2,3,2
7,119,1,3,3
8,119,0,3,4
9,119,0,2,5
10,118,1,1,6
11,118,1,0,7
12,118,0,1,7
13,118,0,1,7
14,118,0,1,7
15,118,0,0,8
16,118,0,0,8
17,118,0,0,8
18,118,0,0,8
19,118,0,0,8
20,118,0,0,8
21,118,0,0,8
22,118,0,0,8
23,118,0,0,8
24,118,0,0,8
25,118,0,0,8
26,118,0,0,8
27,118,0,0,8
28,118,0,0,8
29,117,1,0,8

It only saves data from the last run, I don't know how I can get it to save for all the runs.

1 Answers

to_csv default mode is w as for "writing". It means it's override a file name "file_name.csv" in folder everytime it does this action. So only the last result will be saved.

two options:

  1. Save different file each time. For example, send i to setup_model and then save file as "file_name"+i+".csv". Then you wil have separate file for each epoch.
  2. Use append mode: df.to_csv("file_name.csv", mode="a", header=True). This will append the current dataframe to whatever is in file_name.csv. It won't override it. You will have a single file with all of your results, but it may be inconvenient to separate each epoch.

Choose what is more convenient in your opinion.

Related