# Reduce DataFrame by unique value

**URL:** <https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [October 14, 2022, 10:46am UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723 "2022-10-14T10:46:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 14, 2022, 10:46am UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723/1 "2022-10-14T10:46:02Z")

</div>

Hi all,

I have a large DataFrame with a column of values and a unique id. I would like to create a new DataFrame of the unique values in one column, and a vector of the corresponding ids in another column. Here is an example:

**DataFrame**

```julia
3×2 DataFrame
 Row │ id value 
     │ Int64 Int64 
─────┼──────────────
   1 │ 2 3
   2 │ 4 3
   3 │ 5 1

```

**New DataFrame**

```julia
 Row │ id value 
     │ Array… Int64 
─────┼───────────────
   1 │ [2, 4] 3
   2 │ [5] 1

```

One approach might be to group the DataFrame by value and extract the ids for each group using `combine`. However, I am not quite sure how to do that. Any guidance would be appreciated.

**MWE**

```julia
using DataFrames

df = DataFrame(id = [2,4,5], value = [3,3,1])

groups = groupby(df, :value)

# how do I extract the ids?

df_new = combine(groups, )

```

---

<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:** [October 14, 2022, 10:48am UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723/2 "2022-10-14T10:48:53Z")

</div>

```julia
julia> using DataFrames

julia> df = DataFrame(id = [2, 4, 5], value = [3, 3, 1])
3×2 DataFrame
 Row │ id value
     │ Int64 Int64
─────┼──────────────
   1 │ 2 3
   2 │ 4 3
   3 │ 5 1

julia> combine(groupby(df, :value), :id => Ref)
2×2 DataFrame
 Row │ value id_Ref
     │ Int64 SubArray…
─────┼──────────────────
   1 │ 1 [5]
   2 │ 3 [2, 4]

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 14, 2022, 10:55am UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723/3 "2022-10-14T10:55:35Z")

</div>

> [@nilshg](#):
>
> `combine(groupby(df, :value), :id => Ref)`

Thank you! This helps me out so much. I doubt I would have eventually settled on a solution so simple.

---

<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:** [October 14, 2022, 11:36am UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723/4 "2022-10-14T11:36:07Z")

</div>

this is perhaps more intuitive

```julia
combine(groupby(df,:value), :id=>x->[x])

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 14, 2022, 12:07pm UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723/5 "2022-10-14T12:07:16Z")

</div>

or  
`combine(groupby(df, :value), :id => fill)`  
(which kind-of reads nicely - at least for me, as we ask to fill the cell with values)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 14, 2022, 12:09pm UTC](https://discourse.julialang.org/t/reduce-dataframe-by-unique-value/88723/6 "2022-10-14T12:09:35Z")

</div>

Thank you all for the alternative solutions. I think they are all good. I do agree that `:id => fill` is very readable.
