# Adding a column to a dataframe and conditionally filling based on another column within the same dataframe

**URL:** <https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 14, 2024, 10:09pm UTC](https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413 "2024-09-14T22:09:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocarroll1](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@cocarroll1](https://discourse.julialang.org/u/cocarroll1)\
**Post date:** [September 14, 2024, 10:09pm UTC](https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413/1 "2024-09-14T22:09:30Z")

</div>

I’m trying to write code to do the following, I’m completely new to Julia and coding in general so really struggling.  
I have a data frame of columns and rows, I need to add a new column (EVID) to the df and conditionally fill based on the values in a different column (AMT). The conditional rule is “if AMT is missing then EVID is equal to 0. If AMT is not missing then EVID is equal to 1”

I think what I’m stumbling on is doing two things at once, i.e. inserting a new column (EVID) and then conditionally filling it based on the contents of a different column.

---

<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 14, 2024, 10:17pm UTC](https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413/2 "2024-09-14T22:17:06Z")

</div>

Here’s a solution with DataFramesMeta.jl

```julia
julia> using DataFramesMeta, DataFrames

julia> df = DataFrame(AMT = [1, 4, 3, missing, 3, 2])
6×1 DataFrame
 Row │ AMT     
     │ Int64?  
─────┼─────────
   1 │ 1
   2 │ 4
   3 │ 3
   4 │ missing 
   5 │ 3
   6 │ 2

julia> @rtransform df :EVID = ismissing(:AMT) ? 0 : 1
6×2 DataFrame
 Row │ AMT EVID  
     │ Int64? Int64 
─────┼────────────────
   1 │ 1 1
   2 │ 4 1
   3 │ 3 1
   4 │ missing 0
   5 │ 3 1
   6 │ 2 1

```

---

<div class="post-metadata">

**Author:** ![cocarroll1](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@cocarroll1](https://discourse.julialang.org/u/cocarroll1)\
**Post date:** [September 14, 2024, 10:29pm UTC](https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413/3 "2024-09-14T22:29:52Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![drizk1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drizk1/32/208422_2.png) [@drizk1](https://discourse.julialang.org/u/drizk1)\
**Post date:** [September 15, 2024, 2:45am UTC](https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413/4 "2024-09-15T02:45:37Z")

</div>

TidierData.jl is another option that would look like this

```julia
julia> using TidierData

julia> df = DataFrame(AMT = [1, 4, 3, missing, 3, 2])
6×1 DataFrame
 Row │ AMT     
     │ Int64?  
─────┼─────────
   1 │ 1
   2 │ 4
   3 │ 3
   4 │ missing 
   5 │ 3
   6 │ 2

julia> @mutate(df, EVID = if_else(ismissing(AMT), 0, 1))
6×2 DataFrame
 Row │ AMT EVID  
     │ Int64? Int64 
─────┼────────────────
   1 │ 1 1
   2 │ 4 1
   3 │ 3 1
   4 │ missing 0
   5 │ 3 1
   6 │ 2 1

```

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [September 15, 2024, 12:32pm UTC](https://discourse.julialang.org/t/adding-a-column-to-a-dataframe-and-conditionally-filling-based-on-another-column-within-the-same-dataframe/119413/5 "2024-09-15T12:32:08Z")

</div>

Or just broadcast `ifelse.()`

```julia
using DataFrames
df = DataFrame(AMT = [1, 4, 3, missing, 3, 2])
df.EVID = ifelse.(ismissing.(df.AMT), 0, 1)

```

```julia
6×2 DataFrame
 Row │ AMT EVID  
     │ Int64? Int64 
─────┼────────────────
   1 │ 1 1
   2 │ 4 1
   3 │ 3 1
   4 │ missing 0
   5 │ 3 1
   6 │ 2 1

```
