Iterating over a DataFrame

Hey everyone!
I’m really struggling with a basic concept and was hoping anyone can help me (and that I explain it well enough). Here goes…
I have imported a simple 4x5 dataframe into julia and am trying to apply a function to every row in the dataframe and have the results of every function be readable as an array.
So the thing is though I need to use the elements in every row as part of the function to be able to feed it into the iterator (for-loop).
This is what I’ve got so far and hope it can make it a bit easier to understand:

for i in eachrow(data)

y = (i,2)/(i,3)+(i,1)*(i,5)+(i,4)

i++

return y

end

I hope my problem is understandable and would appreciate any help!

This doesn’t seem to be Julia code - i++ is more of a C idiom?

Assuming you have a function like f(a, b, c, d, e) which takes five inputs, and you have a dataframe with five columns, you can broadcast it as f.(df.col1, df.col2, df.col3, df.col4, df.col5), or even f.(eachcol(df)....):

julia> f(a, b, c, d, e) = sum([a, b, c, d, e])
f (generic function with 1 method)

julia> df = DataFrame(rand(4, 5), :auto)
4×5 DataFrame
 Row │ x1         x2         x3        x4         x5        
     │ Float64    Float64    Float64   Float64    Float64   
─────┼──────────────────────────────────────────────────────
   1 │ 0.225735   0.354707   0.305804  0.552215   0.429499
   2 │ 0.255513   0.577137   0.469197  0.0328176  0.836187
   3 │ 0.647246   0.514615   0.459837  0.136826   0.316774
   4 │ 0.0287949  0.0450149  0.427482  0.743331   0.0578125

julia> f.(eachcol(df)...)
4-element Vector{Float64}:
 1.867960038332126
 2.170851964841856
 2.075297260894371
 1.302435038633726
1 Like

Hey Nils,
thanks for your quick response thank you for your detailed code, this is very helpful!
I’ll post any updates incase I run into trouble :slight_smile: