# Way to return multiple columns after applying combine-groupby transformation

**URL:** https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806
**Category:** New to Julia
**Tags:** question, dataframes
**Created:** [February 26, 2024, 9:12pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806 "2024-02-26T21:12:38Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![JBH\_84](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JBH\_84](https://discourse.julialang.org/u/JBH_84)
#### Post date: [February 26, 2024, 9:12pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/1 "2024-02-26T21:12:38Z")

</div>

I have the following df that i apply a transformation on. it only returns columns y and x however. is there a way to also return the corresponding values in columns a and b?

```julia
df=DataFrame(y=[1, 1, 2, 2],x=[3, 4, 5, 6],a=[1, 1, 1, 1],b=[2, 2, 2, 2])
combine(groupby(df,[:y]), [:x] .=> maximum; renamecols=false)

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 26, 2024, 9:17pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/2 "2024-02-26T21:17:18Z")

</div>

`groupby(df, [:y, :a, :b])`

---

<div class="post-metadata">

### Author: ![JBH\_84](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JBH\_84](https://discourse.julialang.org/u/JBH_84)
#### Post date: [February 26, 2024, 9:47pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/3 "2024-02-26T21:47:20Z")

</div>

sorry. i should have clarified: i dont want to groupby :a and :b. i only want the corresponding values from those columns when i groupby :y and then find the maximum value of :x for each unique value of :y

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 26, 2024, 10:26pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/4 "2024-02-26T22:26:58Z")

</div>

I don’t love this but it works

```julia
julia> combine(groupby(df, :y), 
    [:x, :a, :b] => ((x, a, b) -> let i = argmax(x); (;x=x[i], a=a[i], b=b[i]) end) => AsTable)
2×4 DataFrame
 Row │ y x a b     
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 1 4 1 2
   2 │ 2 6 1 2

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 26, 2024, 11:31pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/5 "2024-02-26T23:31:55Z")

</div>

Another option:

```julia
DataFrame([argmax(t->(t.x),eachrow(q)) for q in groupby(df, [:y])])

```

giving:

```julia
2×4 DataFrame
 Row │ y x a b     
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 1 4 1 2
   2 │ 2 6 1 2

```

---

<div class="post-metadata">

### Author: ![kdpsingh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdpsingh/32/215018_2.png) [@kdpsingh](https://discourse.julialang.org/u/kdpsingh)
#### Post date: [February 27, 2024, 12:29am UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/6 "2024-02-27T00:29:10Z")

</div>

My preferred approach:

```julia
using Statistics

df |>
    x -> groupby(x, :y) |>
    x -> subset(x, :x => (x -> x .== maximum(x)))

```

```julia
2×4 DataFrame
 Row │ y x a b     
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 1 4 1 2
   2 │ 2 6 1 2

```

Or using TidierData:

```julia
using TidierData
using Statistics

@chain df begin
    @group_by(y)
    @filter(x == maximum(x))
    @ungroup
end

# The ungroup is required because only `@summarize()` operations ungroup data frames automatically.

```

```julia
2×4 DataFrame
 Row │ y x a b     
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 1 4 1 2
   2 │ 2 6 1 2

```

---

<div class="post-metadata">

### Author: ![JBH\_84](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JBH\_84](https://discourse.julialang.org/u/JBH_84)
#### Post date: [February 27, 2024, 1:43am UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/8 "2024-02-27T01:43:18Z")

</div>

Wow! Thank you. Very elegant

---

<div class="post-metadata">

### Author: ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)
#### Post date: [February 27, 2024, 1:50am UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/9 "2024-02-27T01:50:45Z")

</div>

```julia
let
    df = DataFrame(y=[1, 1, 2, 2],x=[3, 4, 5, 6],a=[1, 1, 1, 1],b=[2, 2, 2, 2])
    gdf = groupby(df, :y)
    unique(transform(gdf, :x => maximum, renamecols = false))
end

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 27, 2024, 6:37pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/10 "2024-02-27T18:37:50Z")

</div>

> [@George9000](#):
>
> `unique(transform(gdf, :x => maximum, renamecols = false))`

This will not work properly if the `a` and `b` fields are not constant as in this example (as the rows won’t be replicas).

> [@kdpsingh](#):
>
> ```julia
> @filter(x == maximum(x))
> 
> ```

If there are multiple lines attaining the maximum value in `x`, it might output more than one line per group. It isn’t clear what the OP wants, but it may pose a problem.

Another method (which isn’t sensitive to above issues):

```julia
julia> DataFrame(last(g) for g in groupby(sort(df, [:y, :x]),:y))
2×4 DataFrame
 Row │ y x a b     
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 1 4 1 2
   2 │ 2 6 1 2

```

(but this depends on sort stability of `groupby` within groups, which seems to work, but I’m not sure is guaranteed).

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 27, 2024, 6:55pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/11 "2024-02-27T18:55:36Z")

</div>

```julia
combine(gr->gr[gr.x .== maximum(gr.x),:],groupby(df,:y))

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 27, 2024, 7:01pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/12 "2024-02-27T19:01:06Z")

</div>

And another version, (inspired by rocco’s post):

```julia
combine(last, groupby(sort(df, [:y, :x]),:y))

```

(doesn’t return multuple rows/group in case of ties)

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 27, 2024, 7:02pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/13 "2024-02-27T19:02:26Z")

</div>

I believe that the basic operation of the combine(groupby(df,[:cols…]), …) function limits the output to the key columns and the columns acted upon by the func() because there is no a unique criterion for choosing which values of the other columns to output.  
In the case in question the problem does not exist since :a and :b have a constant value.  
So you could do this to get the desired result.

```julia
combine(groupby(df,[:y,:a,:b]),:x=>maximum)

```

---

<div class="post-metadata">

### Author: ![kdpsingh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdpsingh/32/215018_2.png) [@kdpsingh](https://discourse.julialang.org/u/kdpsingh)
#### Post date: [February 27, 2024, 7:13pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/14 "2024-02-27T19:13:41Z")

</div>

> [@kdpsingh](#):
>
> ```julia
> @chain df begin
> @group_by(y)
> @filter(x == maximum(x))
> @ungroup
> end
> 
> ```

If you don’t want ties, then it’s:

```julia
@chain df begin
    @group_by(y)
    @filter(x == maximum(x))
    @slice(1) # for first value, or @slice(n()) for last
    @ungroup
end

```

Or, for short:

```julia
@chain df begin
    @group_by(y)
    @slice_max(x, with_ties = false)
    @ungroup
end

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 27, 2024, 8:28pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/15 "2024-02-27T20:28:53Z")

</div>

```julia
combine(groupby(df,:y),:x=>maximum,[:a,:b].=>first.=>[:a,:b])

combine(groupby(df,:y),:x=>maximum,Not([:x,:y]).=>first.=>identity )

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 27, 2024, 9:04pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/16 "2024-02-27T21:04:53Z")

</div>

> [@rocco\_sprmnt21](#):
>
> ```julia
> combine(groupby(df,:y),:x=>maximum,[:a,:b].=>first.=>[:a,:b])
> 
> ```

Doesn’t work quite right. Consider:

```julia
df=DataFrame(y=[1, 1, 2, 2],x=[3, 4, 5, 6],a=[1, 2,3,4],b=[2, 3,4,5])

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 27, 2024, 9:27pm UTC](https://discourse.julialang.org/t/way-to-return-multiple-columns-after-applying-combine-groupby-transformation/110806/17 "2024-02-27T21:27:14Z")

</div>

Yes, certainly. This form is only for the specific case proposed as an example by the OP. This is exactly what I was referring to in the previous comment. Any choice of non-key, non-transformed column values is arbitrary.
