How to update the empty Pandas dataframe from the the sum of the bottom x rows of a column from an another Pandas dataframe

Viewed 39

I would like to sum the bottom x rows of each column of the dataframe and update it in a another empty data frame.

I tried the below code but i could not update the dataframe.

The master DataFrame is ‘df_new_final’ and it contains numerical values. I want to update in a ‘df_new_final_tail’ as an input of the sum of tail 15 rows from Master DataFrame. But df_new_final_tail is still an empty but i can see that ‘sum_x’ is getting calculated. Not sure why it is not getting updated.

Master DataFrame ——> df_new_final
Child DataFrame ——-> df_new_final_tail

df_series_list = df_series.columns.values.tolist() 
df_new_final_tail = pd.DataFrame(columns=df_series_list)

for items in df_series_list:
    sum_x = df_new_final.tail(15)[items+’_buy’].sum()
    df_new_final_tail[items]=sum_x

Thanks

1 Answers

Convert Series after sum to one column DataFrame by Series.to_frame and for one row DataFrame use transpose by DataFrame.T:

df_new_final_tail = df_new_final.tail(15).sum().to_frame().T

If df_series is another DataFrame and columns names are same with suffix _buy for parse df_new_final use:

items = df_series.columns
df_new_final_tail = df_new_final.tail(15)[items+'_buy'].sum().to_frame().T
Related