# Sum of columns in julia dataframe and appen it as last element in the respective columns

**URL:** <https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837>\
**Category:** General Usage\
**Tags:** dataframes, ijulia, sum\
**Created:** [September 6, 2022, 12:45pm UTC](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837 "2022-09-06T12:45:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [September 6, 2022, 12:45pm UTC](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837/1 "2022-09-06T12:45:18Z")

</div>

I have a dataframe df,

```julia
using DataFrames
df=DataFrame(p1 = [1, 2, 3, 4, 5],p2 = [10,20,30,40,50],p3 = [5,5,10,20,20])

```

I wanted to calculate the sum of specific columns (p1 and p2) and append it as the last row in respective columns (zero on other locations). I wanted to get like

```julia

  Row │ p1 p2 p3    
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 10 5
   2 │ 2 20 5
   3 │ 3 30 10
   4 │ 4 40 20
   5 │ 5 50 20
   6 │ 15 150 0

```

---

<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:** [September 6, 2022, 12:48pm UTC](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837/2 "2022-09-06T12:48:57Z")

</div>

Here is one solution

```julia
julia> df = DataFrame(p1 = [1, 2, 3, 4, 5],p2 = [10,20,30,40,50],p3 = [5,5,10,20,20]);

julia> function push_sums!(df, cols)           
           d = Dict(names(df) .=> 0) 
           for col in cols
               d[col] = sum(df[:, col])
           end
           push!(df, d)
       end;

julia> push_sums!(df, ["p1", "p2"])6×3 DataFrame
 Row │ p1 p2 p3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 10 5
   2 │ 2 20 5
   3 │ 3 30 10
   4 │ 4 40 20
   5 │ 5 50 20
   6 │ 15 150 0

```

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [September 6, 2022, 12:49pm UTC](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837/3 "2022-09-06T12:49:24Z")

</div>

I am sure there are more fancy solutions, but this works

```julia
push!(df, (sum(df.p1), sum(df.p2), 0))

```

---

<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:** [September 6, 2022, 2:17pm UTC](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837/4 "2022-09-06T14:17:33Z")

</div>

```julia

using DataFrames
df=DataFrame(p1 = [1, 2, 3, 4, 5],p2 = [10,20,30,40,50],p3 = [5,5,10,20,20], p4 = [5,5,10,20,20], p5 = [5,5,10,20,20],p6 = [5,5,10,20,20])
julia> tot=combine(df, [:p1,:p2].=>sum; renamecols=false)
1×2 DataFrame
 Row │ p1 p2    
     │ Int64 Int64
─────┼──────────────
   1 │ 15 150

julia> vcat(df,tot, cols=:union)
6×6 DataFrame
 Row │ p1 p2 p3 p4 p5 p6      
     │ Int64 Int64 Int64? Int64? Int64? Int64?
─────┼──────────────────────────────────────────────────
   1 │ 1 10 5 5 5 5
   2 │ 2 20 5 5 5 5
   3 │ 3 30 10 10 10 10
   4 │ 4 40 20 20 20 20
   5 │ 5 50 20 20 20 20
   6 │ 15 150 missing missing missing missing

julia> tot=combine(df, [:p1,:p2].=>sum, Cols(∉(["p1","p2"])).=>x->0, renamecols=false)
1×6 DataFrame
 Row │ p1 p2 p3 p4 p5 p6    
     │ Int64 Int64 Int64 Int64 Int64 Int64
─────┼──────────────────────────────────────────
   1 │ 15 150 0 0 0 0

julia> vcat(df,tot)
6×6 DataFrame
 Row │ p1 p2 p3 p4 p5 p6    
     │ Int64 Int64 Int64 Int64 Int64 Int64
─────┼──────────────────────────────────────────
   1 │ 1 10 5 5 5 5
   2 │ 2 20 5 5 5 5
   3 │ 3 30 10 10 10 10
   4 │ 4 40 20 20 20 20
   5 │ 5 50 20 20 20 20
   6 │ 15 150 0 0 0 0

```
