python - 将文件名的标题添加到csv
问题描述
所以我遍历多个 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
解决方案
推荐阅读
- iphone - UserDefaults 不加载
- node.js - eslint-plugin-import:适用于非 svg 或 css 的 js 文件
- node.js - 在 Mac 上打开 Electron 应用程序时出现问题 - “git 命令需要命令行开发人员工具。”
- css - 将离子图标 SVG 缩放到 div 尺寸
- java - 如何实现 Spring Method 级别的安全性
- c++ - 在 C++ 中,我可以在不本地声明的情况下将结构作为指针传递吗?
- reactjs - rc-ranger - 正确处理范围值
- c# - 模型属性上的 EF [必需] 注释但跳过验证 - 覆盖 ModelValidationState?
- r - 一个一个地计算向量中多个数字的出现
- javascript - Material-ui Button onClick() 导致“Uncaught TypeError: Cannot read property 'name' of undefined”错误