# Convert dictionary to Tables.table

**URL:** https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920
**Category:** New to Julia
**Tags:** question
**Created:** [March 15, 2019, 5:09pm UTC](https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920 "2019-03-15T17:09:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [March 15, 2019, 5:09pm UTC](https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920/1 "2019-03-15T17:09:00Z")

</div>

I have a dictionary where the values are a named tuple:

```julia
julia> d = Dict("x" => (a = 1, b = 2), "y" => (a = 3, b = 4))
Dict{String,NamedTuple{(:a, :b),Tuple{Int64,Int64}}} with 2 entries:
  "x" => (a = 1, b = 2)
  "y" => (a = 3, b = 4)

```

**How can I convert that to a `Tables.table`?** Needless to say, I’d need to specify the name of the first column, the one expressed by the keys of the dictionary, while the other columns will be named after the names of the fields of the named tuple.

To be honest, while it would be great to know _that_, my ultimate goal is to save `d` as a CSV file (which is easy to do once I have the `table`).

Thanks!

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [March 15, 2019, 5:53pm UTC](https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920/2 "2019-03-15T17:53:17Z")

</div>

This works:

```julia
function totable(d::Dict{R, NamedTuple{S,T}}, firstname::Symbol) where {R,S,T}
    names = [firstname, S...]
    renamecol(table(d), [i => name for (i, name) in enumerate(names)])
end

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [March 15, 2019, 5:54pm UTC](https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920/3 "2019-03-15T17:54:10Z")

</div>

> [@yakir12](#):
>
> Dict(“x” =\> (a = 1, b = 2), “y” =\> (a = 3, b = 4))

The easier is probably to convert it to something that iterates named tuples:

```julia
itr = (merge((; key = key), val) for (key, val) in d)
itr |> CSV.write("test.csv")

```

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [March 15, 2019, 6:06pm UTC](https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920/4 "2019-03-15T18:06:19Z")

</div>

Just another option from @joshday (via slack):

```julia
 renamecol(table(d), [1=>:key, 2=>:a, 3=>:b])

```

---

<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 15, 2019, 8:48pm UTC](https://discourse.julialang.org/t/convert-dictionary-to-tables-table/21920/5 "2019-03-15T20:48:47Z")

</div>

Here is the [Queryverse](https://www.queryverse.org/) variant of this:

```julia
using Query, CSVFiles

d |> @map({key=_[1], _[2]...}) |> save("foo.csv")

```

Or you can pipe it into a `DataFrame` or some other table type.
