Home > Mobile >  Pandas - Groupby rename column name
Pandas - Groupby rename column name

Time:02-17

I am trying to rename the new aggregated column name in the groupBy function.

My Code:

groupByColumns = ['clientId', 'state', 'branchId']
aggColumn = 'amount'
aggOperation = sum
comNewColName = totalSalesDone

result = df.groupby(groupByColumns)[aggColumn].agg(aggOperation)

this here it is working perfectly. Now I am trying to rename the aggeregated new column

result = df.groupby(groupByColumns, as_index=False).agg(comNewColName=(aggColumn,aggOperation))

But I am getting column name comNewColName but I need to get totalSalesDone.

how can I get that please.

CodePudding user response:

I would just get rid of "comNewColName = totalSalesDone" result['totalSalesDone'] = df.groupby(groupByColumns, as_index=False).agg(aggColumn,aggOperation))

CodePudding user response:

You are passing sum, but should pass 'sum' (as a string) in a grouped named aggregation. That should solve your issue. So I believe changing:

aggOperation = sum

to:

aggOperation = 'sum'

And doing:

result = df.groupby(groupByColumns, as_index=False).agg(comNewColName=(aggColumn,aggOperation))

Should work.

CodePudding user response:

You can unpack a dictionary in Named Aggregation

Here is an example:

from seaborn import load_dataset    
df_tips = load_dataset('tips')

aggcol = 'total_bill'
newColumnSum='Sum_Total_Bill'
aggop = 'sum'
df_tips.groupby('sex')[[aggcol]].agg(**{f'{newColumnSum}':(f'{aggcol}',f'{aggop}')})

Output:

        Sum_Total_Bill
sex                   
Male           3256.82
Female         1570.95
  • Related