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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With