Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convenient way to convert dataframe to vector of tuple?

I'm trying to make a function that converts dataframe to vector of tuples in Julia.

For example,

using DataFrames
df = DataFrame(A=1:4, B=4:7, C=10:13)

4×3 DataFrame
 Row │ A      B      C     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      4     10
   2 │     2      5     11
   3 │     3      6     12
   4 │     4      7     13

T = [t for t in zip(df.A, df.B, df.C)]

T = 4-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 4, 10)
 (2, 5, 11)
 (3, 6, 12)
 (4, 7, 13)

Then T becomes the result what I exactly wanted.

The problem is, however, that I need to functionalize above process.

So, what I need is to automatically put columns of dataframe into the zip function.

The form of function that I want to make is as below

using DataFrames

function DataframeToTuple(df)
    T = [t for t in zip(df.first column name, df.second column name, ... df.last column name)]
    return T
end

Is there any convenient way? Thanks a lot

like image 476
c0000000kie Avatar asked Oct 14 '25 19:10

c0000000kie


1 Answers

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 NamedTuples 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)
like image 78
Przemyslaw Szufel Avatar answered Oct 17 '25 16:10

Przemyslaw Szufel