Convenient way to convert dataframe to vector of tuple?
This is perhaps the shortest way:
julia> Tuple.(eachrow(df))
4-element Vector{Tuple{Int64, Int64, Int64}}:
(1, 4, 10)
(2, 5, 11)
(3, 6, 12)
(4, 7, 13)
It is also quite interesting to know that you can convert a DataFrame
to a Vector
of NamedTuple
s in an identical way:
julia> NamedTuple.(eachrow(df))
4-element Vector{NamedTuple{(:A, :B, :C), Tuple{Int64, Int64, Int64}}}:
(A = 1, B = 4, C = 10)
(A = 2, B = 5, C = 11)
(A = 3, B = 6, C = 12)
(A = 4, B = 7, C = 13)
A more efficient way to convert a data frame to a vector of NamedTuple
if it is not very wide (roughly less than 1000 columns) is:
julia> Tables.rowtable(df)
4-element Vector{NamedTuple{(:A, :B, :C), Tuple{Int64, Int64, Int64}}}:
(A = 1, B = 4, C = 10)
(A = 2, B = 5, C = 11)
(A = 3, B = 6, C = 12)
(A = 4, B = 7, C = 13)
and if you insist on tuple then do Tuple.(Tables.rowtable(df))
.