Home > Net >  Pandas: More than one aggregate on a single column but display not adjacent
Pandas: More than one aggregate on a single column but display not adjacent

Time:04-17

I am trying to achieve the below.

'''
SELECT genre
    , sum(price)
    , count(*)
    , avg(price) 
FROM books 
GROUP BY genre
'''

This works:

books.groupby('genre').agg({'price': [np.sum, np.mean], 'genre': np.size})

This also works:

books.groupby('genre').agg({'price': [np.sum, np.mean, np.size]})

However, above the sum and mean aggregates are adjacent which I do not want. There should be the 'Count' between the two.

I am not able to understand the exact syntax if I want to display the 'Count' between the two. Could you help, please?

Thank you.

Kind regards,

Manoj.

CodePudding user response:

fair question...I don't know how to do it from the get-go...but I guess I would just rearrange your columns as desired after the fact:

df_agg=df.groupby('genre').agg({'price': [np.sum, np.mean], 'genre': np.size})

cols2=[('price','sum'),('genre','size'),('price','mean')]
df_agg[cols2]
  • Related