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

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

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

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     │
3 Likes