# How to convert namedarray to dataframe while preserving index/column names

**URL:** https://discourse.julialang.org/t/how-to-convert-namedarray-to-dataframe-while-preserving-index-column-names/34357
**Category:** General Usage
**Created:** [February 8, 2020, 9:12pm UTC](https://discourse.julialang.org/t/how-to-convert-namedarray-to-dataframe-while-preserving-index-column-names/34357 "2020-02-08T21:12:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)
#### Post date: [February 8, 2020, 9:12pm UTC](https://discourse.julialang.org/t/how-to-convert-namedarray-to-dataframe-while-preserving-index-column-names/34357/1 "2020-02-08T21:12:22Z")

</div>

Hi, I’m trying to make a pivot table in Julia that aggregates the count of something. Freqtable is perfect, except that it returns this namedarray format, and when I use to convert back to a dataframe, the index and column names of the namedarray are gone. How should I go about this? I literally want the namedarray, exactly as is with index and column labels, in a Julia DataFrame

---

<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: [February 8, 2020, 10:45pm UTC](https://discourse.julialang.org/t/how-to-convert-namedarray-to-dataframe-while-preserving-index-column-names/34357/2 "2020-02-08T22:45:41Z")

</div>

This will only work if `NamedArray` is two dimensional (but I guess it is in your case):

```julia
julia> using DataFrames

julia> using FreqTables

julia> using NamedArrays

julia> ft = freqtable(rand('a':'d', 100), rand('x':'z',100))
4×3 Named Array{Int64,2}
Dim1 ╲ Dim2 │ 'x' 'y' 'z'
────────────┼──────────────
'a' │ 6 12 9
'b' │ 8 9 12
'c' │ 11 6 9
'd' │ 9 4 5

julia> df = DataFrame(ft, Symbol.(names(ft, 2)))
4×3 DataFrame
│ Row │ x │ y │ z │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 6 │ 12 │ 9 │
│ 2 │ 8 │ 9 │ 12 │
│ 3 │ 11 │ 6 │ 9 │
│ 4 │ 9 │ 4 │ 5 │

julia> insertcols!(df, 1, Symbol(dimnames(ft, 1)) => names(ft, 1))
4×4 DataFrame
│ Row │ Dim1 │ x │ y │ z │
│ │ Char │ Int64 │ Int64 │ Int64 │
├─────┼──────┼───────┼───────┼───────┤
│ 1 │ 'a' │ 6 │ 12 │ 9 │
│ 2 │ 'b' │ 8 │ 9 │ 12 │
│ 3 │ 'c' │ 11 │ 6 │ 9 │
│ 4 │ 'd' │ 9 │ 4 │ 5 │

```
