# Using DataFrames \`combine\` is there a way to programmatically pass multiple functions to apply to the same same column?

**URL:** <https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238>\
**Category:** Data\
**Created:** [January 19, 2023, 11:08pm UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238 "2023-01-19T23:08:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 19, 2023, 11:08pm UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/1 "2023-01-19T23:08:14Z")

</div>

Is there a way to pass a Vector{Function} like this:  
`methods = [mean, max]`  
to DataFrames combine? I was thinking something like this?  
`df = combine(groupdf, nrow, 3 => methods)`  
of  
`df = combine(groupdf, nrow, 3 => methods...)`

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 19, 2023, 11:19pm UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/2 "2023-01-19T23:19:40Z")

</div>

try broadcasting over the functions with `.=>` maybe?

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 19, 2023, 11:25pm UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/3 "2023-01-19T23:25:11Z")

</div>

brilliant, so intuitive, thanks… I love Julia 😉

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 19, 2023, 11:58pm UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/4 "2023-01-19T23:58:07Z")

</div>

any suggestions for programmatically applying multiple methods to multiple columns?

for

`methods = [mean, maximum]`

and

`cols = [:x, :y]`

`df = combine(groupdf, cols .=> methods)` gives results of `x_mean` and `y_maximum`

`df = combine(groupdf, cols[1] .=> methods)` gives results of `x_mean` and `x_maximum`

`df = combine(groupdf, cols[2] .=> methods)` gives results of `y_mean` and `y_maximum`

but I can’t seem to find a solution that gives `x_mean` , `x_maximum`, `y_mean` , `y_maximum`

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 20, 2023, 12:39am UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/5 "2023-01-20T00:39:21Z")

</div>

the third solution here worked:

> <https://stackoverflow.com/questions/66350756/multiple-summary-statistics-on-grouped-column-in-julia>

```julia
 combine(gdf, nrow, [n => f for n in cols for f in methods])

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 20, 2023, 12:39am UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/6 "2023-01-20T00:39:32Z")

</div>

make a poor man’s cartesian product

```julia
julia> df = DataFrame([:a => [1,2,3], :b => [4,5,6]])
3×2 DataFrame
 Row │ a b     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 4
   2 │ 2 5
   3 │ 3 6

julia> select(df, repeat([:a, :b], inner=2) .=> repeat([maximum, minimum], outer=2))
3×4 DataFrame
 Row │ a_maximum a_minimum b_maximum b_minimum 
     │ Int64 Int64 Int64 Int64     
─────┼────────────────────────────────────────────
   1 │ 3 1 6 4
   2 │ 3 1 6 4
   3 │ 3 1 6 4

```

the generator you found looks good too!

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [January 20, 2023, 12:44am UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/7 "2023-01-20T00:44:36Z")

</div>

The simplest way is to change `methods = [mean, maximum]` for `methods = [mean maximum]` (no comma). Then, if you run `combine(groupdf, cols .=> methods)` you get `x_mean` , `x_maximum` , `y_mean` , `y_maximum`.

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 20, 2023, 12:51am UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/8 "2023-01-20T00:51:49Z")

</div>

This is fantastic… are you able to explain why a `Vector` of functions needs to be iterated over while a `Matrix` of functions does not?

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [January 20, 2023, 1:01am UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/9 "2023-01-20T01:01:12Z")

</div>

You can see more about it here:

> **[Transforming multiple columns with multiple functions in DataFrames.jl](https://bkamins.github.io/julialang/2021/07/09/multicol.html)**
>
> Introduction

Just in case, that blog is written by @bkamins, who is one of the developers of DataFrames. It’s really helpful.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 20, 2023, 3:52am UTC](https://discourse.julialang.org/t/using-dataframes-combine-is-there-a-way-to-programmatically-pass-multiple-functions-to-apply-to-the-same-same-column/93238/10 "2023-01-20T03:52:48Z")

</div>

> [@alex-s-gardner](#):
>
> while a `Matrix` of functions does not?

See:

```julia
julia> ["r1", "r2", "r3"] .=> ["c1" "c2"]
3×2 Matrix{Pair{String, String}}:
 "r1"=>"c1" "r1"=>"c2"
 "r2"=>"c1" "r2"=>"c2"
 "r3"=>"c1" "r3"=>"c2"

```
