# Dataframes: Split combined result to different columns

**URL:** https://discourse.julialang.org/t/dataframes-split-combined-result-to-different-columns/73023
**Category:** General Usage
**Tags:** dataframes
**Created:** [December 13, 2021, 1:37pm UTC](https://discourse.julialang.org/t/dataframes-split-combined-result-to-different-columns/73023 "2021-12-13T13:37:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TBuConst](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TBuConst](https://discourse.julialang.org/u/TBuConst)
#### Post date: [December 13, 2021, 1:37pm UTC](https://discourse.julialang.org/t/dataframes-split-combined-result-to-different-columns/73023/1 "2021-12-13T13:37:23Z")

</div>

Hello everyone,

I’m pretty new to Julia and struggle with a data wrangling problem. Say, I’ve got the following dataframe:

```julia
using DataFrames
df = DataFrame(ID = [1,1,1,2,2,2,3,3],
               OP = [:X,:X,:Y,:X,:Y,:Y,:X,:Y],
               COUNT = [5, 10, 2, 7, 2, 0, 1, 2])

```

What I want to get is for each ID the count according to OP X or Y. What I can do is to use `groupby` and `combine` to aggregate the data:

```julia
gdf = groupby(df, [:ID, :OP])
result_df = combine(gdf, :COUNT => sum)

```

This creates a dataframe, in which for each ID I get two rows - row 1 contains the count for X and row 2 the count for Y. Is there an easy way to get the counts for X and Y in two different columns? Instead of `result_df` created in the last code chunk I’d like to get the following dataframe.

```julia
optimal_DF = DataFrame(ID = [1,2,3],
                       X_COUNT = [15,7,1],
                       Y_COUNT = [2,2,2])

```

If there is an easy and idiomatic way to do this, I’d like to hear about it.

Regards,

Thomas

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 13, 2021, 1:41pm UTC](https://discourse.julialang.org/t/dataframes-split-combined-result-to-different-columns/73023/2 "2021-12-13T13:41:53Z")

</div>

I think you want `unstack`. See the following

```julia
julia> @chain df begin 
           groupby([:ID, :OP])
           @combine :count_sum = sum(:COUNT)
           unstack(:ID, :OP, :count_sum)
       end
3×3 DataFrame
 Row │ ID X Y      
     │ Int64 Int64? Int64? 
─────┼───────────────────────
   1 │ 1 15 2
   2 │ 2 7 2
   3 │ 3 1 2

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 13, 2021, 1:43pm UTC](https://discourse.julialang.org/t/dataframes-split-combined-result-to-different-columns/73023/3 "2021-12-13T13:43:34Z")

</div>

You could do

```julia
julia> combine(groupby(df, :ID),
           [:OP, :COUNT] => ((op, c) -> sum((op .== :X).*c)) => :X_COUNT,
           [:OP, :COUNT] => ((op, c) -> sum((op .== :Y).*c)) => :Y_COUNT)
3×3 DataFrame
 Row │ ID X_COUNT Y_COUNT 
     │ Int64 Int64 Int64   
─────┼─────────────────────────
   1 │ 1 15 2
   2 │ 2 7 2
   3 │ 3 1 2

```

The alternative is to `unstack` after `combine`.

EDIT: Just saw Peter’s answer, that’s the `unstack` version.

---

<div class="post-metadata">

### Author: ![TBuConst](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TBuConst](https://discourse.julialang.org/u/TBuConst)
#### Post date: [December 13, 2021, 1:58pm UTC](https://discourse.julialang.org/t/dataframes-split-combined-result-to-different-columns/73023/4 "2021-12-13T13:58:40Z")

</div>

Hi,

thanks to both of your for the quick answers - it works like a charm!

Regards,

Thomas
