# Recommended equivalent to map / broadcast on GroupedDataFrame

**URL:** <https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819>\
**Category:** Data\
**Created:** [February 27, 2024, 5:07am UTC](https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819 "2024-02-27T05:07:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremiedb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremiedb/32/29150_2.png) [@jeremiedb](https://discourse.julialang.org/u/jeremiedb)\
**Post date:** [February 27, 2024, 5:07am UTC](https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819/1 "2024-02-27T05:07:39Z")

</div>

If we have function `f1` that takes a DataFrame as an argument and returns a scalar, how can a reduction operation using `f1` be applied over each `SubDataFrame` of a grouped dataframe?

```julia
using DataFrames

# reduce function
f1(df::AbstractDataFrame) = sum(df[!, :x1])

# data with `y` as key
df1 = DataFrame(rand(100, 3), :auto)
df1.y = rand(1:3, 100)
df1g = groupby(df1, :y)

```

An ideal scenario would be to be able to directly pass `SubDataFrame` as input to the the aggregating function in `combine`, something like:

```julia
combine(df1g, AsDF() => f1 => :z)

```

The following works, but is an inefficient workaround:

```julia
combine(df1g, AsTable(:) => (x -> f1(DataFrame(x))) => :z)

```

Otherwise, the map and broadcasting seemed natural options but they are reserved operations:

```julia
julia> map(f1, df1g)
ERROR: ArgumentError: using map over `GroupedDataFrame`s is reserved

julia> f1.(df1g)
ERROR: ArgumentError: broadcasting over `GroupedDataFrame`s is reserved
Stacktrace:

```

Going through a loop also appear the efficient option, but doesn’t appear as an “elegant” solution:

```julia
out = Pair[]
for idx in eachindex(df1g)
    push!(out, idx => f1(df1g[idx]))
end

```

Is there a more straighforward way to performed the desired reduction over grouped dataframe that I missed?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 27, 2024, 8:42am UTC](https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819/2 "2024-02-27T08:42:36Z")

</div>

If I understood the requirement correctly, you could try the following form

```julia
combine(f1,df1g)

```

if you want to rename the new column, you could use a function like this

```julia
f2(df::AbstractDataFrame) = (;z=sum(df[!, :x1]))

```

---

<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:** [February 27, 2024, 8:49am UTC](https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819/3 "2024-02-27T08:49:26Z")

</div>

Yes - `combine` is the intended method if you want a data frame in the output.  
If you want a vector then do:

```julia
[f(sdf) for sdf in gdf]

```

This ambiguity (the type of the resulting object) is the reason why `map` is not implemented for `GroupedDataFrame`.

---

<div class="post-metadata">

**Author:** ![jeremiedb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremiedb/32/29150_2.png) [@jeremiedb](https://discourse.julialang.org/u/jeremiedb)\
**Post date:** [February 27, 2024, 8:01pm UTC](https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819/4 "2024-02-27T20:01:25Z")

</div>

Thank you! I had completely overlooked the application of the `combine(fun, df)` method as I’ve integrated these verbs to be strictly of the form `combine(df, ops...)`. Much appreciated!

---

<div class="post-metadata">

**Author:** ![nshima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nshima/32/208287_2.png) [@nshima](https://discourse.julialang.org/u/nshima)\
**Post date:** [April 5, 2024, 1:11pm UTC](https://discourse.julialang.org/t/recommended-equivalent-to-map-broadcast-on-groupeddataframe/110819/5 "2024-04-05T13:11:59Z")

</div>

> **Summary**
>
> This text will be hidden

```julia
map(f1,collect(df1g))

```

works fine
