Appending arrays to dataframe (python)

IndigoChild picture IndigoChild · Jan 24, 2018 · Viewed 26.9k times · Source

So I ran a time series model on a small sales data set, and forecasted sales for next 12 periods. With the following code:

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

where df1 contains the sales values with months being the index. Now I'm storing the predicted values in the following manner:

pred.append(y_future)

Now, I need to append the forecasted values to the original dataset df1, preferably with the same index. I'm trying to use the following code:

df1.append(pred, ignore_index=False)

But I'm getting the following error:

TypeError: cannot concatenate a non-NDFrame object

I've tried converting pred variable to list and then appending, but to no avail. Any help will be appreciated. Thanks.

Answer

saloua picture saloua · Jan 24, 2018

One solution could be appending the new array to your dataFrame to the last position using df.loc

df.loc[len(df)] = your_array

But this is not efficient cause if you want to do it several times, it will have to get the length of the DataFrame for each new append.

A better solution would be to create a dictionary of the values that you need to append and append it to the dataFrame.

df = df.append(dict(zip(df.columns, your_array)), ignore_index=True)