Search code examples
pythonfor-loopfacebook-prophet

How to write ouput from for-loop to CSV using Facebook Prophet in Python?


I am trying to use a for-loop in Python in combination with Facebook Prophet forecasting, so I will be able to make a forecast for each product that I want to have a forecast for.

The answer given here is almost perfect: forecasting values for each category using Prophet in python

The answer does not include how to write the output from the loop to a CSV in such a way the topic starter posted in the screenshot.

When I try it myself via:

df_temp = get_prediction(df2)
print(df_temp)

df_output = pd.DataFrame(list(df_temp.items()))
df_output.to_csv('Tutorial.csv', index=True)

I can see the predictions from Prophet. But since it's in a dictionary output, I don't know how to properly get it into a csv. Storing it in a dataframe also doesn't solve things with pd.DataFrame.from_dict.

Any other ideas for this? Usually I can figure stuff out thanks to stackoverflow, but now I'm really stuck.


Solution

  • Welcome to Stackoverflow!

    I think you need to set mode=a in your code to append your prediction into csv file.

    df_output.to_csv('Tutorial.csv', index=True,mode='a')
    

    Refer to link for more information.