1

Say I have a dataframe with columns A, B, C, D. What I want is a new dataframe that would correspond to the SQL query

"SELECT a,b, count(c), sum(d) FROM initial_df GROUP BY a,b"

I'm struggling to figure out how to chain the group by so that I can apply separate aggregate functions to the separate columns, C and D

Milan
  • 344
  • 1
  • 10
  • 1
    IIUC, `df.groupby(['a','b']).agg(c=('c','count'),d=('d','sum')).reset_index()` in pandas `0.25+` or try `df.groupby(['a','b']).agg({'c':'count','d':'sum'}).reset_index()` – anky Jan 24 '20 at 03:17

0 Answers0