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

I have a dataframe df,

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


  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

Here is one solution

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
2 Likes

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

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

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