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     ā”‚
4 Likes