# Row-wise mean of columns in a DataFrame

**URL:** <https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358>\
**Category:** Data\
**Created:** [August 13, 2021, 8:07pm UTC](https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358 "2021-08-13T20:07:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [August 13, 2021, 8:07pm UTC](https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358/1 "2021-08-13T20:07:43Z")

</div>

I saw a post where @bkamins explained how to add a column that has the sum of the rows in selected columns ( [(Julia) Assigning DataFrame column sum to a new column - Stack Overflow](https://stackoverflow.com/questions/64637186/julia-assigning-dataframe-column-sum-to-a-new-column)). Is there a way to use `transform` to do the same, but with the `mean` operation?

I know I can do something like:

```julia
df.mean = mean(Array(df), dims=2)

```

Another way with `transform` is:

```julia
transform(df, names(df) => ByRow((i...) -> mean(i)))

```

Is there a cleaner way to do this with `transform`?

---

<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:** [August 13, 2021, 8:11pm UTC](https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358/2 "2021-08-13T20:11:37Z")

</div>

> [@hdavid16](#):
>
> ```julia
> df.mean = mean(Array(df), dims=2)
> 
> ```

Definitely don’t do this. It will allocate tons of memory.

You want

```julia
transform(df, AsTable(:) => ByRow(mean) => :rowmean)

```

BTW, are you coming from Stata by chance? This is a common operation in Stata.

Also, the above will run into trouble if you have many many columns because it constructs a named tuple. Making it better has been the object of extensive discussion. See [here](https://github.com/JuliaData/DataFrames.jl/issues/2768).

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [August 13, 2021, 8:14pm UTC](https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358/3 "2021-08-13T20:14:20Z")

</div>

Awesome, thanks!

Not from Stata, just trying to plot the timeseries output of multiple stochastic simulations (mean +/- 1 std)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 13, 2021, 10:58pm UTC](https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358/4 "2021-08-13T22:58:19Z")

</div>

In this case, broadcasting seems to be faster & allocate less than using transform?

```julia
using DataFrames, BenchmarkTools
df = DataFrame(rand(1000,1000), :auto)
dg = deepcopy(df)
@btime transform($df, AsTable(:) => ByRow(mean) => :rowmean) # 322.4 ms (1015669 allocations: 77.59 MiB)
@btime dg.rowmean .= mean(Array($dg), dims=2) # 1.887 ms (1020 allocations: 7.67 MiB)

```

---

<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:** [August 13, 2021, 11:58pm UTC](https://discourse.julialang.org/t/row-wise-mean-of-columns-in-a-dataframe/66358/5 "2021-08-13T23:58:49Z")

</div>

Yeah, `1000` is definitely large enough for `AsTable` to cause problems.

```julia
reduce(+, eachcol(df)) ./ ncol(df)

```

should be faster than both.
