将文件名标题添加到csv

时间:2018-12-27 14:35:44

标签: python pandas csv facebook-prophet

因此,我正在遍历多个csv文件,并使用FBProphet获得它们的预测。然后,我使用.to_csv将所有预测写入一个csv文件。但是我想获取文件名以将它们在csv上分开,并且不确定使用.to_csv时是否有办法做到这一点。

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('final_data.csv',mode='a',header=True)

所以我得到的结果是

ds yhat yhat_lower yhat_upper                                    
example1 forecast

ds yhat yhat_lower yhat_upper                                   
example2 forecast

ds yhat yhat_lower yhat_upper                                    
example 3 forecast

我想要得到的结果是

example 1 filename    
ds yhat yhat_lower yhat_upper                                   
example1 forecast

example 2 filename                                                    
ds yhat yhat_lower yhat_upper    
example2 forecast

example 3 filename   
ds yhat yhat_lower yhat_upper  
example 3 forecast

0 个答案:

没有答案