# Sum the columns in a dataframe

**URL:** <https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077>\
**Category:** New to Julia\
**Created:** [August 17, 2020, 6:10am UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077 "2020-08-17T06:10:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)\
**Post date:** [August 17, 2020, 6:10am UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/1 "2020-08-17T06:10:07Z")

</div>

This should be a relatively simple question but I could not find a great answer. I have a dataframe like below ( but with way more colums), and I wish to add a new column that is the sum of all the other columns (e.g. a + b + c). We can assume that all of the columns are float64 types. I would like to do this over a large group of columns so specifying each column one by one is not feasible. Many thanks!

```julia
df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),

                      b = repeat([2, 1], outer=[4]),

                                     c = randn(8))

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 17, 2020, 6:54am UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/2 "2020-08-17T06:54:49Z")

</div>

Not at a computer but I think something like `sum(eachcol(df))` should work

---

<div class="post-metadata">

**Author:** ![derekmahar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derekmahar/32/216439_2.png) [@derekmahar](https://discourse.julialang.org/u/derekmahar)\
**Post date:** [August 17, 2020, 9:20am UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/3 "2020-08-17T09:20:46Z")

</div>

That seems to work:

```julia
julia> using DataFrames

julia> df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),

                             b = repeat([2, 1], outer=[4]),

                                            c = randn(8))
8×3 DataFrame
│ Row │ a │ b │ c │
│ │ Int64 │ Int64 │ Float64 │
├─────┼───────┼───────┼───────────┤
│ 1 │ 1 │ 2 │ 2.33499 │
│ 2 │ 2 │ 1 │ 1.08153 │
│ 3 │ 3 │ 2 │ 1.55002 │
│ 4 │ 4 │ 1 │ -1.35953 │
│ 5 │ 1 │ 2 │ -1.87585 │
│ 6 │ 2 │ 1 │ 1.06405 │
│ 7 │ 3 │ 2 │ -0.129446 │
│ 8 │ 4 │ 1 │ 1.98992 │

julia> sum(eachcol(df))
8-element Array{Float64,1}:
 5.334989587238981
 4.081530272951787
 6.550024757198643
 3.640465366532979
 1.1241529384736229
 4.064050523357361
 4.8705544804273035
 6.989923186107735

```

---

<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 17, 2020, 1:36pm UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/4 "2020-08-17T13:36:56Z")

</div>

Are you coming from Stata, by chance? You can emulate `rowtotal` using `ByRow` in `transform` in DataFrames.

---

<div class="post-metadata">

**Author:** ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)\
**Post date:** [August 17, 2020, 11:33pm UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/5 "2020-08-17T23:33:48Z")

</div>

I am coming from an R background…

Thank you the eachcol solution works. I tried transform, but probably wansnt getting the syntax right.

---

<div class="post-metadata">

**Author:** ![MalteMederacke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltemederacke/32/18305_2.png) [@MalteMederacke](https://discourse.julialang.org/u/MalteMederacke)\
**Post date:** [October 2, 2020, 12:14pm UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/6 "2020-10-02T12:14:49Z")

</div>

Doesn’t this sum the rows? Sum of the column sum(eachrow(df)) doesnt work for me. Why not?

---

<div class="post-metadata">

**Author:** ![bert](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@bert](https://discourse.julialang.org/u/bert)\
**Post date:** [June 24, 2021, 2:32pm UTC](https://discourse.julialang.org/t/sum-the-columns-in-a-dataframe/45077/7 "2021-06-24T14:32:36Z")

</div>

`sum(eachcol(df))` does indeed sum across, since it essentially does `sum([df[!, c] for c in names(df)])`. If you want to sum down, you should use `sum.(eachcol(df))`, which is essentially `[sum(df[!, c]) for c in names(df)]`.
