# Iterating over a DataFrame

**URL:** https://discourse.julialang.org/t/iterating-over-a-dataframe/61890
**Category:** New to Julia
**Tags:** iterative, dataframes, function
**Created:** [May 26, 2021, 9:34pm UTC](https://discourse.julialang.org/t/iterating-over-a-dataframe/61890 "2021-05-26T21:34:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mo\_re](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@mo\_re](https://discourse.julialang.org/u/mo_re)
#### Post date: [May 26, 2021, 9:34pm UTC](https://discourse.julialang.org/t/iterating-over-a-dataframe/61890/1 "2021-05-26T21:34:04Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 26, 2021, 9:55pm UTC](https://discourse.julialang.org/t/iterating-over-a-dataframe/61890/2 "2021-05-26T21:55:06Z")

</div>

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
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

```

---

<div class="post-metadata">

### Author: ![mo\_re](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@mo\_re](https://discourse.julialang.org/u/mo_re)
#### Post date: [May 26, 2021, 10:16pm UTC](https://discourse.julialang.org/t/iterating-over-a-dataframe/61890/3 "2021-05-26T22:16:30Z")

</div>

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 🙂
