# DataFrames.jl - Vectorized row-wise function application

**URL:** <https://discourse.julialang.org/t/dataframes-jl-vectorized-row-wise-function-application/14513>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [September 4, 2018, 1:50am UTC](https://discourse.julialang.org/t/dataframes-jl-vectorized-row-wise-function-application/14513 "2018-09-04T01:50:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdsalerno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsalerno/32/5539_2.png) [@mdsalerno](https://discourse.julialang.org/u/mdsalerno)\
**Post date:** [September 4, 2018, 1:50am UTC](https://discourse.julialang.org/t/dataframes-jl-vectorized-row-wise-function-application/14513/1 "2018-09-04T01:50:10Z")

</div>

What are efficient ways to row-wise apply a function `f` over a DataFrame? Looking through the DataFrames.jl documentation, I have found plenty of examples of column-wise aggregation. However, say that I want to take one or more columns, apply a function to each row, and output an array-like structure containing the results for each row, similar to Julia’s native vectorized dot notation for arrays:

`f.(df[:A]) # something like this`

Thus far, I have been doing as follows:

`f.(vec(convert(Array, df[:A])))`

Is this a reasonable way to go about it?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 4, 2018, 2:44am UTC](https://discourse.julialang.org/t/dataframes-jl-vectorized-row-wise-function-application/14513/2 "2018-09-04T02:44:17Z")

</div>

I think what you are doing is right or check out [this functionality in DataFramesMeta.jl](https://github.com/JuliaStats/DataFramesMeta.jl#byrow)

```julia
using DataFrames
df = DataFrame(a=1:3,b=4:6)
f(a,b) = a+b
f.(df[:a],df[:b])

# or
using DataFramesMeta
@with(df, f.(:a,:b))

```

---

<div class="post-metadata">

**Author:** ![mdsalerno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsalerno/32/5539_2.png) [@mdsalerno](https://discourse.julialang.org/u/mdsalerno)\
**Post date:** [September 4, 2018, 4:17am UTC](https://discourse.julialang.org/t/dataframes-jl-vectorized-row-wise-function-application/14513/3 "2018-09-04T04:17:11Z")

</div>

Thanks xiaodai. I was making a mistake with the dot notation which you helped me identify.

DataFramesMeta looks quite useful!

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [October 13, 2018, 10:06pm UTC](https://discourse.julialang.org/t/dataframes-jl-vectorized-row-wise-function-application/14513/4 "2018-10-13T22:06:22Z")

</div>

[Query.jl](https://github.com/queryverse/Query.jl) also lets you do this. For example to run a function `f` over the columns `a` and `b`:

```julia
df |> @map(f(_.a, _.b)) |> collect

```

Will return a vector of the results.
