# UnStack dataframe with aggregated string column

**URL:** <https://discourse.julialang.org/t/unstack-dataframe-with-aggregated-string-column/81072>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [May 14, 2022, 7:32pm UTC](https://discourse.julialang.org/t/unstack-dataframe-with-aggregated-string-column/81072 "2022-05-14T19:32:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [May 14, 2022, 7:32pm UTC](https://discourse.julialang.org/t/unstack-dataframe-with-aggregated-string-column/81072/1 "2022-05-14T19:32:13Z")

</div>

Hello

Can someone let me know if this is possible

I have a dataframe like this

```julia
> julia> df = DataFrame(x=["a","a","b"],y=["x","x","y"],z=["l","m","n"])
> 3×3 DataFrame
> Row │ x y z      
> │ String String String 
> ─────┼────────────────────────
> 1 │ a x l
> 2 │ a x m
> 3 │ b y n

```

I would like to reshape it like this

```julia
X Y Zagg
----------------
a x l,m
b y n

```

Is this supported with dataframes?

I’d also like to make sure that the ZAgg column strings are sorted so that l,m and m,l aren’t aggregated as two different values

---

<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:** [May 14, 2022, 7:48pm UTC](https://discourse.julialang.org/t/unstack-dataframe-with-aggregated-string-column/81072/2 "2022-05-14T19:48:42Z")

</div>

Yes, it is possible:

```julia
julia> combine(groupby(df, [:x, :y]), :z => x -> join(sort(x), ","))
2×3 DataFrame
 Row │ x y z_function
     │ String String String
─────┼────────────────────────────
   1 │ a x l,m
   2 │ b y n

```

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [May 14, 2022, 8:06pm UTC](https://discourse.julialang.org/t/unstack-dataframe-with-aggregated-string-column/81072/3 "2022-05-14T20:06:52Z")

</div>

Awesome! Just what I needed although I did add a unique to the list which worked fine.

Thanks.

---

<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:** [May 14, 2022, 9:44pm UTC](https://discourse.julialang.org/t/unstack-dataframe-with-aggregated-string-column/81072/4 "2022-05-14T21:44:48Z")

</div>

with a handcrafted join

```julia
combine(groupby(df,[:x,:y]),:z=>x->chop(*(x.*","...)))

```

or

```julia
combine(groupby(df,[:x,:y]),:z=>x->chop(*(string.(x,",")...)))

```
