# Using previous row values to create values for a new column

**URL:** <https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [March 18, 2021, 3:25pm UTC](https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481 "2021-03-18T15:25:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alessio](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alessio](https://discourse.julialang.org/u/alessio)\
**Post date:** [March 18, 2021, 3:25pm UTC](https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481/1 "2021-03-18T15:25:49Z")

</div>

I have this DataFrame:

`df = DataFrame(a = rand(Float64, 10))`

I would like to add a second Integer column, `b`, whose value is `1` for the first row and, for the other rows, takes the result of a function that requires inputs from the previous row, as well as the current row.

Pseudo code:  
`df.b = if first row then 1 else (if df.a < 0.5 then previous row's df.b else previous row's df.b + 1)`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 18, 2021, 3:33pm UTC](https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481/2 "2021-03-18T15:33:26Z")

</div>

looks like you have order dependency thus the task is not columnar. you can do it in a loop:

```julia
julia> b = similar(df.a, Int); b[1] = 1;

julia> for i in 2:10
           b[i] = b[i-1] + (df.a[i] >= 0.5)
       end

julia> df.b = b

```

---

<div class="post-metadata">

**Author:** ![alessio](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alessio](https://discourse.julialang.org/u/alessio)\
**Post date:** [March 18, 2021, 4:02pm UTC](https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481/3 "2021-03-18T16:02:24Z")

</div>

Thank you.  
I was also thinking about cloning the `a` column and _shift it 1 row_, but that would be inefficient and after all avoiding the loop wouldn’t make my code more elegant anyway

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 18, 2021, 4:31pm UTC](https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481/4 "2021-03-18T16:31:22Z")

</div>

While direct method is no doubt efficient, if you are coming from an SQL world, you probably want to use something like ShiftedArrays.jl which has `lag` and `lead` functions: [ANN: ShiftedArrays and support for ShiftedArrays in GroupedErrors](https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162)

---

<div class="post-metadata">

**Author:** ![alessio](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alessio](https://discourse.julialang.org/u/alessio)\
**Post date:** [March 20, 2021, 3:18pm UTC](https://discourse.julialang.org/t/using-previous-row-values-to-create-values-for-a-new-column/57481/5 "2021-03-20T15:18:06Z")

</div>

If I create a ShiftedArray for a DataFrame column, would the shifted array be a copy of the data frame column?  
I guess this question is equivalent to asking if data frame columns are backed by arrays as their storage representation
