Dataframe destructors

What is the best (simple/efficient) way to transform a dataframe into a namedtuple of vectors?
and
What is the best (simple/efficient) way to transform a dataframe into a vector of namedtuples?

julia> using DataFrames
julia> using Tables

julia> df = DataFrame(a=rand(3), b=rand(3))
3Ɨ2 DataFrame
 Row ā”‚ a          b        
     ā”‚ Float64    Float64  
ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€
   1 ā”‚ 0.266649   0.430507
   2 ā”‚ 0.0831088  0.612815
   3 ā”‚ 0.181096   0.658077

julia> Tables.columntable(df)
(a = [0.2666485657643721, 0.08310884238994776, 0.18109579692899447], b = [0.4305065586341045, 0.6128151086401142, 0.6580774728835025])

julia> Tables.rowtable(df)
3-element Vector{NamedTuple{(:a, :b), Tuple{Float64, Float64}}}:
 (a = 0.2666485657643721, b = 0.4305065586341045)
 (a = 0.08310884238994776, b = 0.6128151086401142)
 (a = 0.18109579692899447, b = 0.6580774728835025)

Not necessarily the most efficient, but super clean.

5 Likes

Without Tables.jl,

NamedTuple(pairs(eachcol(df))) # NT of vectors
NamedTuple.(eachrow(df)) # vector of NTs
4 Likes