0

So I have several csv files that I am importing in and then I use FB Prophet to give me a forecast for the coming months data. I would like all of the forecasts to go to either a different csv or all on the same one. Currently it is only writing the last csv of filenames to a csv and not doing the others.

filenames=['example1.csv','example2.csv','example3.csv']

for f in filenames:
    df=pd.read_csv(f)
    df.columns=['ds','y']
    df['ds']=pd.to_datetime(df['ds'])
    m=Prophet(seasonality_mode='multiplicative')
    m.fit(df)

    future=m.make_future_dataframe(periods=6,freq='M')
    forecast=m.predict(future)
    forecast[['ds','yhat','yhat_lower','yhat_upper']].tail()
    fig1=m.plot(forecast)
    fig2=m.plot_components(forecast)

    forecast.to_csv(f)

I expect all of the forecasts to be on one csv or have a new csv created for each filename that is looped through.

merv
  • 67,214
  • 13
  • 180
  • 245

2 Answers2

2

in pandas, .to_csv() is in write mode by default but has an option to change it. In your example, you are iterating through filenames when writing to_csv(), so as it is currently written, you will overwrite your current files with the results from your code. If you'd like to write these dataframes to a new file, with each df appended to the bottom:

forecast.to_csv('final_data.csv', mode='a')
d_kennetz
  • 5,219
  • 5
  • 21
  • 44
  • @d_d_kennetz: How should one approach when there are multiple subgropus present in data frame and the output is needed in csv or Excel file with multiple tabe/sheet with group name? See: https://stackoverflow.com/questions/55545501/how-to-perform-time-series-analysis-that-contains-multiple-groups-in-python-usin – biggboss2019 Apr 21 '19 at 01:14
1

You can use .to_csv() with mode="a" to accomplish your task or you can open the file in append mode and write to it using to_csv().

Using .to_csv() with append mode

df.to_csv("file_name.csv", mode="a")

Opening a file in append mode and using to_csv to write to it.

with open("filename.csv", 'a') as file:
    df.to_csv(file, header=False)
Command
  • 515
  • 2
  • 6
  • 19