# Apply some functions to columns of a dataframe

**URL:** <https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [November 9, 2021, 6:39pm UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216 "2021-11-09T18:39:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [November 9, 2021, 6:39pm UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216/1 "2021-11-09T18:39:52Z")

</div>

Hello,  
I’m a beginner in Julia.  
Here is my problem; i have:

1. a dataframe with 4 columns and 10 rows
2. 3 functions u1, u2, u3
3. a vector of 3 weights

And i want to create a function (arguments : the dataframe, the 3 functions and the weights ) this function must return a vector built as follows

- we apply the function u1 on values of column 2, the function u2 on values of column 3, the function u3 on values of column 4
- then we do, for each row, a weighted sum with vector w
- so, we have at the end a vector with 10 values

I don’t know how to do.  
Thanks for your help.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 9, 2021, 7:11pm UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216/2 "2021-11-09T19:11:03Z")

</div>

Have you worked through any tutorials on [DataFrames.jl](https://github.com/bkamins/Julia-DataFrames-Tutorial) or [DataFramesMeta.jl](https://juliadata.github.io/DataFramesMeta.jl/stable/).

This is very easy to do with those tools.

```julia
julia> using DataFrames, DataFramesMeta

julia> df = DataFrame(u1 = rand(10), u2 = rand(10), u3 = rand(10))
10×3 DataFrame
 Row │ u1 u2 u3       
     │ Float64 Float64 Float64  
─────┼────────────────────────────────
   1 │ 0.226441 0.46347 0.803374
   2 │ 0.698363 0.367743 0.452573
   3 │ 0.768411 0.168 0.6769
   4 │ 0.0897352 0.959212 0.551368
   5 │ 0.952901 0.407275 0.125132
   6 │ 0.951374 0.327541 0.100814
   7 │ 0.00756429 0.377843 0.60469
   8 │ 0.274698 0.1681 0.874693
   9 │ 0.174282 0.36525 0.805875
  10 │ 0.319033 0.424992 0.686173

julia> f1(x) = x .+ 1; f2(x) = x .+ 2; f3(x) = x .+ 3;

julia> w = [.5, .2, .3];

julia> @transform df :z = w[1] * f1(:u1) + w[2] * f2(:u2) + w[3] * f3(:u3)
10×4 DataFrame
 Row │ u1 u2 u3 z       
     │ Float64 Float64 Float64 Float64 
─────┼─────────────────────────────────────────
   1 │ 0.226441 0.46347 0.803374 2.24693
   2 │ 0.698363 0.367743 0.452573 2.3585
   3 │ 0.768411 0.168 0.6769 2.42088
   4 │ 0.0897352 0.959212 0.551368 2.20212
   5 │ 0.952901 0.407275 0.125132 2.39545
   6 │ 0.951374 0.327541 0.100814 2.37144
   7 │ 0.00756429 0.377843 0.60469 2.06076
   8 │ 0.274698 0.1681 0.874693 2.23338
   9 │ 0.174282 0.36525 0.805875 2.20195
  10 │ 0.319033 0.424992 0.686173 2.25037

```

---

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [November 10, 2021, 7:55am UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216/3 "2021-11-10T07:55:29Z")

</div>

Thanks for your help and for the links of tutorials !

---

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [November 10, 2021, 10:04am UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216/4 "2021-11-10T10:04:35Z")

</div>

I come back to you.  
It works for all functions but not for functions obtained by linear Interpolation as below

```julia
using Interpolations
x1=sort(vf[1][:,2])
y1=reverse(vf[1][:,1])
f1=LinearInterpolation(x1,y1)

```

where vf[1] is a 3x2 matrix

The error is :  
BoundsError: attempt to access 3-element extrapolate(interpolate((::Vector{Float64},), ::Vector{Float64}, Gridded(Linear())), Throw()) with element type Float64 at index [2]

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 10, 2021, 12:03pm UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216/5 "2021-11-10T12:03:10Z")

</div>

This is a very different question. Please post it in a new thread.

Additionally, take a look at a little [guide](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) for asking informative questions.

---

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [November 10, 2021, 12:25pm UTC](https://discourse.julialang.org/t/apply-some-functions-to-columns-of-a-dataframe/71216/6 "2021-11-10T12:25:22Z")

</div>

OK; i will post a new thread.  
Thanks !
