# How to create \`DataFrame\` from using NamedTuple keys as column names

**URL:** <https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405>\
**Category:** Data\
**Created:** [August 11, 2019, 2:41am UTC](https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405 "2019-08-11T02:41:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 11, 2019, 2:41am UTC](https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405/1 "2019-08-11T02:41:07Z")

</div>

I have a function that returns a `NamedTuple` with 7 entries. The names are obviously `Symbols` and the entries are `Int64`. As this function is run many times, I would like to store the returned data as rows in a `DataFrame`, but I am having trouble creating a DataFrame to begin with.

Here is a MWE

```julia
  using DataFrames
    ntd = (A = 2, B = 3, C = 4)   
    DataFrame(keys(ntd)) ## OR
    DataFrame(String.(keys(ntd)))

```

which results in error  
`ERROR: ArgumentError: 'NTuple{7,Symbol}' iterates 'Symbol' values, which don't satisfy the Tables.jl Row-iterator interface`

I know this I can manually construct the DataFrame by typing in my column names but is there a way to do this programtically?

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 11, 2019, 2:47am UTC](https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405/2 "2019-08-11T02:47:48Z")

</div>

Should’ve searched Google some more. I found the answer.

```julia
dfnames = [keys(nt)...]
DataFrame([Int64 for i = 1:7], dfnames, 0)

```

where `nt` is my `NamedTuple`.

---

<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:** [August 11, 2019, 3:08am UTC](https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405/3 "2019-08-11T03:08:37Z")

</div>

Even easiert: `DataFrame([ntd])`.

---

<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:** [August 11, 2019, 5:41am UTC](https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405/4 "2019-08-11T05:41:17Z")

</div>

You do not have to initialize a `DataFrame` with columns in this case. You can `push!` a `NamedTuple` even to a `DataFrame` without columns. E.g.

```julia
df = DataFrame()
push!(df, (a=1,b=2))
push!(df, (a=3,b=4))

```

just works.

The only case when you would need this is when you expect values of heterogeneous types in columns (in which case a `NamedTuple` will have an overly narrow type the first time you `push!` it to a `DataFrame`). In this case you can use a constructor expecting a vector of types and a vector of column names, e.g. `ataFrame([Int, Float64], [:a, :b])`.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 11, 2019, 8:50pm UTC](https://discourse.julialang.org/t/how-to-create-dataframe-from-using-namedtuple-keys-as-column-names/27405/5 "2019-08-11T20:50:41Z")

</div>

Thanks. Infact, the columns are heterogeneous in data so just constructing an empty DataFrame didn’t work for me.
