简体   繁体   English

如何通过在单独的 csv 文件中循环迭代来保存每个 output

[英]How to save each output by iteration of a loop in separate csv files

I have a file in which i calculate a startdate and enddate, these dates change by iteration each time between a range of dates.我有一个文件,我在其中计算开始日期和结束日期,这些日期每次在一系列日期之间通过迭代更改。 the code works fine but i don't know how to save each output seperately in csv files.代码工作正常,但我不知道如何将每个 output 分别保存在 csv 文件中。

My current code saves the first output of the loop only and save it in different csv files.我当前的代码仅保存循环的第一个 output,并将其保存在不同的 csv 文件中。

from pandas.tseries.offsets import MonthEnd
from datetime import datetime, timedelta,date
import numpy as np
from pathlib import Path
import glob
import pandas as pd



for firstdate in pd.date_range('2022-01-01', '2025-12-31', freq='MS'):
startdate = firstdate.date()
enddate = (startdate + MonthEnd(1)).date()
df['slicing_startdate'] = np.where(((df.contractstartdate.dt.date.isnull()) | 
 (df.contractstartdate.dt.date < startdate)), startdate, df.contractstartdate.dt.date)
df['slicing_enddate'] = np.where(((df.effectiveenddate.dt.date.isnull()) | (enddate < 
df.effectiveenddate.dt.date)), enddate, df.effectiveenddate.dt.date)

folder_path= r".../FileName.csv"
months = ['M1','M2','M3','M4','M5','M6','M7','M8','M9','M10','M11','M12','M13','M14','M15','M16','M17','M18','M19','M20','M21','M22','M23','M24','M25','M26','M27','M28','M29','M30','M31','M32','M33','M34','M35','M36']
    for month in months:
    p = Path(folder_path).parent.joinpath(f"BD_{month}.csv")
    if not p.is_file():
        df.to_csv(p,index=False)
    else:
        print('file exists')

So in each csv file under the name of BD_M1, BD_M2, ....etc i have the same slicingstardate = 01/01/2022 and slicingenddate = 31/01/2022.因此,在名称为 BD_M1、BD_M2 等的每个 csv 文件中,我有相同的 slicingstardate = 01/01/2022 和 slicingenddate = 31/01/2022。 while the desired output is for each csv file i have the corresponding dates.虽然所需的 output 是针对每个 csv 文件,但我有相应的日期。

thanks a lot for your help!非常感谢你的帮助!

You could remove the second for loop and use enumerate instead, this way you link each data_range iteration to an increasing counter ( month ) in the same loop.您可以删除第二个 for 循环并改为使用enumerate ,这样您就可以将每个data_range迭代链接到同一循环中递增的计数器 ( month )。

from pandas.tseries.offsets import MonthEnd
from datetime import datetime, timedelta,date
...

...

folder_path= r"../FileName.csv"

for month, firstdate in enumerate(pd.date_range('2022-01-01', '2025-12-31', freq='MS'), 1):
    startdate = firstdate.date()
    enddate = (startdate + MonthEnd(1)).date()
    df['slicing_startdate'] = np.where(((df.contractstartdate.dt.date.isnull()) | 
    (df.contractstartdate.dt.date < startdate)), startdate, df.contractstartdate.dt.date)
    df['slicing_enddate'] = np.where(((df.effectiveenddate.dt.date.isnull()) | (enddate < 
    df.effectiveenddate.dt.date)), enddate, df.effectiveenddate.dt.date)

    p = Path(folder_path).parent.joinpath(f"BD_M{month}.csv")
    if not p.is_file():
        df.to_csv(p,index=False)
    else:
        print('file exists')

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

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