Appending arrays to dataframe (python) Appending arrays to dataframe (python) arrays arrays

Appending arrays to dataframe (python)


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)


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.

Steps to follow

  • First find the max day in datetime column of your actual data frame and convert it to datetime. We want to assign future dates for our forecasting results.
  • Create an empty dictionary list and inside a loop fill it by incrementing datetime value 1 day and place a forecasted result subsequently.
  • Append that dictionary list to your dataframe. Don't forget to reassign it to itself as left hand value since append function creates a copy of appended results data frame.
  • Reindex your data frame.

Code

lastDay = dfActualData[dfActualData['datetime'] == dfActualData['datetime'].max()].values[0][0]dtLastDay = lastDay.to_pydatetime("%Y-%m-%d")listdict = []for i in range(len(results)):    forecastedDate = dtLastDay + timedelta(days = i + 1)    listdict.append({'datetime':forecastedDate , 'value':results[i]})dfActualData= dfActualData.append(listdict, ignore_index=True)dfActualData.reset_index(drop=True)