# \[DataFrames Question\]: How to convert single column with row of dictionary to multiple columns

**URL:** <https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989>\
**Category:** Specific Domains\
**Tags:** question, dataframes\
**Created:** [May 13, 2022, 3:19am UTC](https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989 "2022-05-13T03:19:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Bringuier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefan_bringuier/32/20132_2.png) [@Stefan\_Bringuier](https://discourse.julialang.org/u/Stefan_Bringuier)\
**Post date:** [May 13, 2022, 3:19am UTC](https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989/1 "2022-05-13T03:19:20Z")

</div>

# Question:

If I have a dataframe with a column that has rows which are dictionaries, whats the appropriate way to convert each `key` to a column and fill the elements with the `value`. I assume its through the [`transform!`](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.transform!) function, but I’m not sure exactly how to do this.

# Example:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/750d64e5875fe8d1974452578266bf7db5b71324.png)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 13, 2022, 5:09am UTC](https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989/2 "2022-05-13T05:09:20Z")

</div>

Here is one way (interested to see other approaches):

```julia
## Setup
using DataFrames
df = DataFrame(x = [Dict(:y => 1.0, :z => 2.0), Dict(:yz=>1.0, :y=>0.0, :z=>0.5)])

## Scan first to get list of possible column names:
keylist = Set()

for row in eachrow(df)
   push!(keylist, keys(row.x)...)
end

## Now create the transformed dataframe:
for k in keylist
    df[!,k] = [haskey(row.x,k) ? row.x[k] : missing for row in eachrow(df)]
end

```

---

<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:** [May 13, 2022, 7:19am UTC](https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989/3 "2022-05-13T07:19:12Z")

</div>

`DataFrame` constructors accept `Dict`s, so the simplest is probably:

```julia
julia> reduce(vcat, DataFrame.(df.x), cols = :union)
2×3 DataFrame
 Row │ y z yz
     │ Int64 Int64 Int64?
─────┼───────────────────────
   1 │ 1 2 missing
   2 │ 4 5 3

```

which you can then just hcat onto your existing table:

```julia
julia> df = DataFrame(x = [Dict(:y => 1, :z => 2), Dict(:yz => 3, :y => 4, :z => 5)])
2×1 DataFrame
 Row │ x
     │ Dict…
─────┼────────────────────────────
   1 │ Dict(:y=>1, :z=>2)
   2 │ Dict(:yz=>3, :y=>4, :z=>5)

julia> hcat(df, reduce(vcat, DataFrame.(df.x), cols = :union))
2×4 DataFrame
 Row │ x y z yz
     │ Dict… Int64 Int64 Int64?
─────┼───────────────────────────────────────────────────
   1 │ Dict(:y=>1, :z=>2) 1 2 missing
   2 │ Dict(:yz=>3, :y=>4, :z=>5) 4 5 3

```

---

<div class="post-metadata">

**Author:** ![Stefan\_Bringuier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefan_bringuier/32/20132_2.png) [@Stefan\_Bringuier](https://discourse.julialang.org/u/Stefan_Bringuier)\
**Post date:** [May 14, 2022, 3:44pm UTC](https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989/4 "2022-05-14T15:44:27Z")

</div>

@nilshg and @jd-foster thanks a bunch, this does what I need. I checked the timings with `@time`, no significant difference I see. Is there a reason not to do this with `transform`?

---

<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:** [May 14, 2022, 6:01pm UTC](https://discourse.julialang.org/t/dataframes-question-how-to-convert-single-column-with-row-of-dictionary-to-multiple-columns/80989/5 "2022-05-14T18:01:39Z")

</div>

`transform` needs matching keys for each row, so if your second `Dict` has `:yz` as a key, it won’t work.
