# Sum rows of DataFrame

**URL:** <https://discourse.julialang.org/t/sum-rows-of-dataframe/97920>\
**Category:** Data\
**Tags:** question, dataframes\
**Created:** [April 25, 2023, 9:54pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920 "2023-04-25T21:54:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 9:54pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/1 "2023-04-25T21:54:43Z")

</div>

What is the most idiomatic way to sum up all rows of a DataFrame into a row of totals? Can you give an example where the DataFrame contains numeric and non-numeric columns?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 25, 2023, 10:03pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/2 "2023-04-25T22:03:51Z")

</div>

How about  
[https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.describe](https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.describe)  
?

---

<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:** [April 25, 2023, 10:09pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/3 "2023-04-25T22:09:37Z")

</div>

Easiest way (when there are non-numeric columns) is

```julia
julia> df = DataFrame(a = [1, 2, 3], b = [5, 6, 7], c = ["x", "y", "z"]);

julia> df.rowsum = sum.(eachrow(df[:, names(df, Real)]))
3-element Vector{Int64}:
  6
  8
 10

```

In the mini-language there is

```julia
julia> df = DataFrame(a = [1, 2, 3], b = [5, 6, 7], c = ["x", "y", "z"]);

julia> transform(df, AsTable(names(df, Real)) => ByRow(sum) => :rowsum)
3×4 DataFrame
 Row │ a b c rowsum 
     │ Int64 Int64 String Int64  
─────┼──────────────────────────────
   1 │ 1 5 x 6
   2 │ 2 6 y 8
   3 │ 3 7 z 10

```

But be warned, the “mini-language” version will make some `NamedTuple`s under the hood, and if you have 1000+ columns this may impact performance.

With DataFramesMeta there is

```julia
julia> @rtransform df :rowtotal = sum(AsTable(names(df, Real)))
3×4 DataFrame
 Row │ a b c rowtotal 
     │ Int64 Int64 String Int64    
─────┼────────────────────────────────
   1 │ 1 5 x 6
   2 │ 2 6 y 8
   3 │ 3 7 z 10

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 10:14pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/4 "2023-04-25T22:14:15Z")

</div>

I think I should provide an example of expected result. I am looking for the sum of rows to produce a final row with the totals, like we do in a spreadsheet:

```julia
julia> df = DataFrame(a=[1,2,3], b=[1,1,1], c=["a","b","c"])

# run magic function
julia> magic(df)

| a | b |
---------
| 6 | 3 |

```

---

<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:** [April 25, 2023, 10:15pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/5 "2023-04-25T22:15:37Z")

</div>

Oh sorry! I thought you meant the opposite

```julia

julia> combine(df, names(df, Real) .=> sum)
1×2 DataFrame
 Row │ a_sum b_sum 
     │ Int64 Int64 
─────┼──────────────
   1 │ 6 3

```

---

<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:** [April 26, 2023, 7:38am UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/6 "2023-04-26T07:38:35Z")

</div>

> [@pdeffebach](#):
>
> But be warned, the “mini-language” version will make some `NamedTuple`s under the hood, and if you have 1000+ columns this may impact performance.

For `ByRow(sum)` it will not create a `NamedTuple` and it should scale.

---

<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:** [April 26, 2023, 3:44pm UTC](https://discourse.julialang.org/t/sum-rows-of-dataframe/97920/8 "2023-04-26T15:44:41Z")

</div>

Ah correct! I forgot about that optimization.
