# Cumulative sum on rows of a dataframe

**URL:** <https://discourse.julialang.org/t/cumulative-sum-on-rows-of-a-dataframe/70242>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [October 22, 2021, 9:12pm UTC](https://discourse.julialang.org/t/cumulative-sum-on-rows-of-a-dataframe/70242 "2021-10-22T21:12:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [October 22, 2021, 9:12pm UTC](https://discourse.julialang.org/t/cumulative-sum-on-rows-of-a-dataframe/70242/1 "2021-10-22T21:12:31Z")

</div>

I am a beginner in Julia…  
I am trying to add a new column with cumulative values in regards with some other columns

```julia
function weighted_sum(input_df::DataFrame, weight::Array{Int64,1}, start_col=2)
    somme_df = deepcopy(input_df)
    l,end_col=size(input_df)
    for i in start_col:end_col
        somme_df[!,i]=weight[i-1]*input_df[!,i]
    end
    somme_df.cumValue = [sum(somme_df[!, j]) for j in start_col:end_col]
    return somme_df
end

```

But i doesn’t work : no new column cumValue is added.

Thanks in advance for your help

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 23, 2021, 5:45am UTC](https://discourse.julialang.org/t/cumulative-sum-on-rows-of-a-dataframe/70242/2 "2021-10-23T05:45:20Z")

</div>

The cumulative section here may help.

> **[DataFramesCheatSheet\_v1.x\_rev1.pdf](https://www.ahsmart.com/assets/pages/data-wrangling-with-data-frames-jl-cheat-sheet/DataFramesCheatSheet_v1.x_rev1.pdf)**
>
> 329.56 KB

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 23, 2021, 8:48am UTC](https://discourse.julialang.org/t/cumulative-sum-on-rows-of-a-dataframe/70242/3 "2021-10-23T08:48:02Z")

</div>

Do you really want a cumulative sum on the rows? From your code it looks that you rather want a linear combination of columns…

Here are some ways to do a linear combination of columns. In these examples we want to combine columns 2,3,4 with respective weights 10,1,0:

```julia
julia> weights = [10, 1, 0];

julia> df = DataFrame(rand(0:9, 3, 4), :auto)
3×4 DataFrame
 Row │ x1 x2 x3 x4    
     │ Int64 Int64 Int64 Int64 
─────┼────────────────────────────
   1 │ 9 4 5 6
   2 │ 1 2 3 9
   3 │ 2 3 4 2

```

One way is using linear algebra, putting the columns in a matrix:

```julia
df.cumValue = Array(df[!, 2:end]) * weights

# or
transform(df, 2:4 => ((cols...) -> hcat(cols...) * weights) => :cumValue)

# result:
3×5 DataFrame
 Row │ x1 x2 x3 x4 cumValue 
     │ Int64 Int64 Int64 Int64 Int64    
─────┼──────────────────────────────────────
   1 │ 9 4 5 6 45
   2 │ 1 2 3 9 23
   3 │ 2 3 4 2 34

```

Another way is to implement the linear combination with `mapreduce`:

```julia
transform(df, 2:4 => ((c...) -> mapreduce(*, +, c, weights)) => :cumValue)

```

I think the best is to define the linear combination as a function:

```julia
linear_combination(weights) = (cols...) -> mapreduce(*, +, cols, weights)

transform(df, 2:4 => linear_combination(weights) => :cumValue)

```

---

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [October 23, 2021, 2:35pm UTC](https://discourse.julialang.org/t/cumulative-sum-on-rows-of-a-dataframe/70242/4 "2021-10-23T14:35:10Z")

</div>

It’s exactly what i need : thanks a lot !
