为什么数据框架只导出迭代的最后一个值到csv?(Python)



我正在将结果从dataframe导出到CSV,但是它只导出迭代的最后一个值。请检查我的代码,让我知道,我在哪里做错了。谢谢您的支持。

from pyswmm import Simulation, LidGroups, Nodes
from pyswmm.swmm5 import SWMMException
import os
import pandas as pd

output_path = "E:VARS_Researchpyswmm_masterTest_ModelTest_Model_Manual"
output_csv_file = "node_flow.csv"
with Simulation('Test_model_LID.inp') as sim:
nodes = Nodes(sim)

for step in sim:
j1 = Nodes(sim)["J1"]
j2 = Nodes(sim)["J2"]
j3 = Nodes(sim)["J3"]
j4 = Nodes(sim)["J4"]
results = {j1.total_inflow, j2.total_inflow, j3.total_inflow, j4.total_inflow} 
sim.step_advance(300)

for i in results:
current_time = sim.current_time
my_df = pd.DataFrame.from_dict({'j1': [j1.total_inflow], 'j2': [j2.total_inflow], 'j3': [j3.total_inflow], 'j4': [j4.total_inflow]})
my_df.to_csv(os.path.join(output_path, "node_flow.csv"))

results的每次迭代中,您写入覆盖前一个文件的csv文件。不确定这是否是您想要的,但在to_csv()中指定mode='a'。默认模式为w

链接到to_csv()文档

相关内容

  • 没有找到相关文章

最新更新