Convert dataframe to array of tuples

Hello,

I have a dataframe of 2 columns reading from csv file, how can I convert each line of dataframe into a tuple, and combine all the tuples to form like Array{Tuple{Float64, Float64}} or Vector{Tuple{Float64, Float64}}?

The simplest way to do it is:

julia> using DataFrames

julia> df = DataFrame(x=1:4, y=2:5)
4Ɨ2 DataFrame
 Row ā”‚ x      y
     ā”‚ Int64  Int64
ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€
   1 ā”‚     1      2
   2 ā”‚     2      3
   3 ā”‚     3      4
   4 ā”‚     4      5

julia> Tuple.(eachrow(df))
4-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (2, 3)
 (3, 4)
 (4, 5)

If your data frame has very many rows instead do:

julia> Tuple.(Tables.namedtupleiterator(df))
4-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (2, 3)
 (3, 4)
 (4, 5)

which is faster for very long tables.

2 Likes