Home > Mobile >  Add Dataframe values to new DataFrame [duplicate]
Add Dataframe values to new DataFrame [duplicate]

Time:10-06

I'm currently trying to append the values from my DataFrame forecast_array to the DataFrame df_temp. I don't get any error messages, but when I try to print out the df_temp, it is empty...
My code:

import pandas as pd
...

def forecast(forecast_array):
    df_temp = pd.DataFrame(columns=['Article Nr', 'Brand','Date', 'Sales base line', 'Sales upper boundaries', 'Sales lower boundaries', 'Simple Prediction of order date', 'Order Value', 'Note'])
    df_temp.append(forecast_array.iloc[0], ignore_index= True)
    print(df_temp)
    return df_temp

The forecast_array DataFrame has the following structure:

                  Date  Sales base line  ... open Orders Reservations
2022-09-30  01.10.2022       979.997823  ...           0            0

Just as information: in the ... are the same columns as in the df_temp DataFrame, the only two columns, which aren't in the forecast_array are ArticleNr and Brand. I have also tried to append the data without the .iloc[0], but it also doesn't work. Thanks for your help!

CodePudding user response:

You have to reassign df_temp: append return a copy, it does not apply change in place:

Change your code:

    df_temp = df_temp.append(forecast_array.iloc[0], ignore_index= True)
    return df_temp

Or

    return df_temp.append(forecast_array.iloc[0], ignore_index= True)

CodePudding user response:

´df.append´ is not an inplace operation and returns a new DataFrame. Therefore you have to reassign df temp to your appended df_temp:

df_temp = df_temp.append(forecast_array.iloc[0], ignore_index= True)
  • Related