Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending arrays to dataframe (python)

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.

like image 672
IndigoChild Avatar asked Jan 24 '18 10:01

IndigoChild


People also ask

How do you put an array into a DataFrame?

How do you convert an array to a DataFrame in Python? To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) .

How do I add values to a DataFrame array in Python?

You can append your results into a dictionary list and then append that dictionary list to data frame. Let's assume that you want to append your ARIMA forecasted results to the end of actual data frame with two columns "datetime" (YYYY-MM-DD) and "value" respectively.

How do you append data to a DataFrame in Python?

Dataframe append syntax Using the append method on a dataframe is very simple. You type the name of the first dataframe, and then . append() to call the method. Then inside the parenthesis, you type the name of the second dataframe, which you want to append to the end of the first.

Can you append a list to a DataFrame in Python?

By using df. loc[index]=list you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using len(df) function. The below example adds the list ["Hyperion",27000,"60days",2000] to the end of the pandas DataFrame.


1 Answers

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)
like image 108
saloua Avatar answered Sep 23 '22 07:09

saloua