UnStack dataframe with aggregated string column


Can someone let me know if this is possible

I have a dataframe like this

> 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

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

Yes, it is possible:

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
1 Like

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


with a handcrafted join