简体   繁体   English

Pandas to_csv 仅保存上次运行我的脚本的数据。 如何创建一个大表来保存我的所有模拟?

[英]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?

I am fairly new to python.我对 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.我目前正在从我的 Model class 运行此代码,以创建我的文件和数据框,包括字典。 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:这是我设置 pd 数据框的代码:

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.我从我的 Launcher.py 启动我的模拟并导入 setup_model 方法。 Below is code from my Launcher.py:下面是我的 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.当我运行我的模拟时,虽然我只从一次运行中获得 output(每次模拟长达 30 天),但我希望从 5 次模拟运行中获得 output。 Here is an example of my current output:这是我当前 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.它只保存上次运行的数据,我不知道如何让它保存所有运行。

to_csv default mode is w as for "writing". to_csv默认模式是w作为“写作”。 It means it's override a file name "file_name.csv" in folder everytime it does this action.这意味着它每次执行此操作时都会覆盖文件夹中的文件名“file_name.csv”。 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" .例如,将i发送到setup_model ,然后将文件另存为"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) .使用 append 模式: df.to_csv("file_name.csv", mode="a", header=True) This will append the current dataframe to whatever is in file_name.csv.这会将 append 当前 dataframe 转换为 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.选择您认为更方便的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM