# DataFrame transformation question

**URL:** <https://discourse.julialang.org/t/dataframe-transformation-question/21742>\
**Category:** Data\
**Tags:** question\
**Created:** [March 11, 2019, 2:37pm UTC](https://discourse.julialang.org/t/dataframe-transformation-question/21742 "2019-03-11T14:37:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [March 11, 2019, 2:37pm UTC](https://discourse.julialang.org/t/dataframe-transformation-question/21742/1 "2019-03-11T14:37:31Z")

</div>

What I want to do is something like “unstack” but I do not know how to do it.

A simple example:

```julia
l = DataFrame(a = ["a", "a", "b", "b"], c = ["ZA", "ZM", "BW", "ZA"]) 
4×2 DataFrame
│ Row │ a │ c │
│ │ String │ String │
├─────┼────────┼────────┤
│ 1 │ a │ ZA │
│ 2 │ a │ ZM │
│ 3 │ b │ BW │
│ 4 │ b │ ZA │

```

I want to convert l to a dataframe that looks something like this:

```julia
| Row | x1 | x2 |
|-----+----+--------------|
| 1 | a | ["ZA", "ZM"] |
| 2 | b | ["BW", "ZA"] |

```

In the end I want to work with `l[:x2]`.  
In the real world the array in x2 will be of unpredictable length.

At the moment I am doing something very inefficient like this on a DataFrame with more than 400000 rows where z is the dataframe and values an array with the unique values that would be `l[:a]` in the above example and `combinations` would be the equivalent of `l[:x2]` in the example above.

```julia
 combinations = []
 
@time for v in values
         push!(combinations, Set(filter(row -> row[:v] == v, z)[:code]))
     end

```

There must be a more efficient way of doing this.

---

<div class="post-metadata">

**Author:** ![amellnik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amellnik/32/137_2.png) [@amellnik](https://discourse.julialang.org/u/amellnik)\
**Post date:** [March 11, 2019, 3:19pm UTC](https://discourse.julialang.org/t/dataframe-transformation-question/21742/3 "2019-03-11T15:19:24Z")

</div>

```julia
by(l, :a, c_values = :c => x -> [unique(x)])
2×2 DataFrame
│ Row │ a │ c_values │
│ │ String │ Array… │
├─────┼────────┼──────────────┤
│ 1 │ a │ ["ZA", "ZM"] │
│ 2 │ b │ ["BW", "ZA"] │

```

Will do the trick.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 11, 2019, 4:30pm UTC](https://discourse.julialang.org/t/dataframe-transformation-question/21742/4 "2019-03-11T16:30:35Z")

</div>

A slightly more concise version with [Query.jl](https://github.com/queryverse/Query.jl) would be this:

```julia
l |> @groupby(_.a) |> @map({a=key(_), cv=unique(_.c)}) |> DataFrame

```
