0

I'm trying to just get the sum of 'ride_value' by this groupby. How do I do that?

df.groupby(['start_lat_3', 'start_lng_3'])['ride_value'].agg(['sum'])

I'm trying to do this:

df['sum'] = df.groupby(['start_lat_3', 'start_lng_3'])['ride_value'].agg(['sum'])

But of course, that doesn't work because the right-hand side is a Pandas DataFrame.

I tried:

df.groupby(['start_lat_3', 'start_lng_3'])['ride_value'].agg(['sum'])[2]
df.groupby(['start_lat_3', 'start_lng_3'])['ride_value'].agg(['sum'])[['sum']]
df.groupby(['start_lat_3', 'start_lng_3'])['ride_value'].agg(['sum'])['sum']
user2205916
  • 3,196
  • 11
  • 54
  • 82

1 Answers1

1

If you want to bring back a full df input, transform

df['sum'] = df.groupby(['start_lat_3', 'start_lng_3'])['ride_value'].transform('sum')

If you want it summarised, agg

df.groupby(['start_lat_3', 'start_lng_3']).agg({'ride_value':'sum'})
wwnde
  • 26,119
  • 6
  • 18
  • 32