Get value of column corresponding to condition being met for another column in a Grouped DataFrame

using DataFrames, DataFramesMeta
df = DataFrame(x = [1,1,1,2,2,2], y = [1,2,3,101,102,103], z = [5, 6, 7, 505, 506, 507])
gdf = groupby(df, :x)
@combine(gdf, :MaxY = maximum(:y))

Above is MWE. I group by x column and find the max of y column and need the corresponding value of z being returned for each group’s max y value.

I am not sure how to get the corresponding z value? Help appreciated

julia> @combine(gdf, :Maxz = :z[argmax(:y)])
2Γ—2 DataFrame
 Row β”‚ x      Maxz
     β”‚ Int64  Int64
─────┼──────────────
   1 β”‚     1      7
   2 β”‚     2    507

(assuming that you have a unique maximum value)

4 Likes