将数组附加到数据框(python)

Ind*_*ild 7 python arrays dataframe pandas sklearn-pandas

所以我在一个小的销售数据集上运行了一个时间序列模型,并预测了接下来 12 个时期的销售额。使用以下代码:

 mod1=ARIMA(df1, order=(2,1,1)).fit(disp=0,transparams=True)
    y_future=mod1.forecast(steps=12)[0]
Run Code Online (Sandbox Code Playgroud)

其中 df1 包含以月份为索引的销售值。现在我按以下方式存储预测值:

pred.append(y_future)
Run Code Online (Sandbox Code Playgroud)

现在,我需要将预测值附加到原始数据集 df1 中,最好使用相同的索引。我正在尝试使用以下代码:

df1.append(pred, ignore_index=False)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

TypeError: cannot concatenate a non-NDFrame object
Run Code Online (Sandbox Code Playgroud)

我试过将 pred 变量转换为列表然后附加,但无济于事。任何帮助将不胜感激。谢谢。

sal*_*oua 12

一种解决方案可能是使用 df.loc

df.loc[len(df)] = your_array
Run Code Online (Sandbox Code Playgroud)

但这不是有效的原因,如果您想多次执行此操作,则必须为每个新追加获取 DataFrame 的长度。

更好的解决方案是创建需要附加的值的字典并将其附加到数据帧。

df = df.append(dict(zip(df.columns, your_array)), ignore_index=True)
Run Code Online (Sandbox Code Playgroud)