0

In a django setting, given a table with columns

`id`,`x`,`y`,`z` 

I need to

  • groupby on y
  • find the maximum value of z in each group
  • join the outcomes back such that I end up with a table reading

    id,x,y,z,max_z

I managed to get the group maxima via

 max_z = table.objects.values('y').annotate(max_z = Max('z'))

However, I am struggling in the "join back" operation.
If I traverse the new query as

 max_z.values('id','x','y','z','max_z')

then the column max_z and z read equal. i.e.

 `max_z == z` for each `id`

thus max_z does not represent anymore the max value for each group.


For instance, in pandas background, the operation here would read

 df['max_z'] = df.groupby('y').z.transform('max')

Any help appreciated

Acorbe
  • 8,367
  • 5
  • 37
  • 66
  • Do you need the `max_z` against each id? – AKS May 04 '16 at 06:48
  • I just recreated an example and ran exactly what you have written and got what I think you wanted. The question is, what are you observing that indicates a problem? An error? Unexpected results? Can you provide a minimal example? – piRSquared May 04 '16 at 06:50
  • @AKS, I do need the `max_z` against each id – Acorbe May 04 '16 at 07:14
  • @piRSquared, in my case ` max_z.values('id','x','y','z','max_z')` does not contain the maxima against each `id`, rather `z==max_z`, which then is not the maximum.. – Acorbe May 04 '16 at 07:15
  • @Acorbe see my answer and help me see understand what you need. – piRSquared May 04 '16 at 07:32
  • @piRSquared.. Thanks. I need to solve the problem in django, though. I put pandas code to try to frame my question the best – Acorbe May 04 '16 at 07:38

0 Answers0