0

I am trying to do the aggregation on different column and different aggregation on same columns also i.e. in below example I am calculating sum on stake_amount and count on log win. However, I need to calculate mean on stake_amount and rename as average in this code.following one is working as long as I am doing one aggregation on stake_amount but as soon as I try to add another aggregation as mean it shows only mean, not sum please help... following is working as one aggregation.

table_name.groupby(['WIN_RETURN_SIZE']).
agg({'LOG_WIN':'count','STAKE_AMOUNT':'sum'})
.rename(columns={'LOG_WIN':'FREQUENCY','STAKE_AMOUNT':'QUANTITY'})
.reset_index()

when I need to do this (get sum and mean on same column with all other aggregation here)

FF_MEGAWAYS.groupby(['WIN_RETURN_SIZE']).
agg({'LOG_WIN':'count','STAKE_AMOUNT':'sum','STAKE_AMOUNT':'mean'}).
rename(columns={'LOG_WIN':'FREQUENCY','STAKE_AMOUNT':'QUANTITY'})
.reset_index()

Any help will be appricated

2 Answers2

0

Use named aggregation for new columns names with tuple by column with specify aggregate function:

(FF_MEGAWAYS.groupby(['WIN_RETURN_SIZE'], as_index=False)
            .agg(FREQUENCY=('LOG_WIN','count'),
                 QUANTITY=('STAKE_AMOUNT','sum'),
                 AVG=('STAKE_AMOUNT','mean')))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Use list of aggregation functions after column name.

FF_MEGAWAYS.groupby(['WIN_RETURN_SIZE']).
agg({'LOG_WIN':'count','STAKE_AMOUNT':['sum','mean']}).
rename(columns={'LOG_WIN':'FREQUENCY','STAKE_AMOUNT':'QUANTITY'})
.reset_index()
Y U
  • 104
  • 3