# How do I create a new dataframe column by dividing individual elements in two dataframe columns?

**URL:** https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931
**Category:** General Usage
**Tags:** dataframes, dataframesmeta
**Created:** [February 22, 2022, 8:08pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931 "2022-02-22T20:08:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)
#### Post date: [February 22, 2022, 8:08pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931/1 "2022-02-22T20:08:30Z")

</div>

I have the following dataframe:

```julia
[ Info: Displaying top ten rows of the pij_df dataframe...
┌ Info: 10×5 DataFrame
│ Row │ MONTH TOPIC_I TOPIC_J JOINT_PROB_SUM DOC_COUNT_SUM
│ │ Int64 String15 String15 Float64 Int64
│ ─────┼───────────────────────────────────────────────────────────
│ 1 │ 12 TOPIC_153 TOPIC_87 0.0380672 979
│ 2 │ 12 TOPIC_81 TOPIC_87 0.0182519 979
│ 3 │ 12 TOPIC_249 TOPIC_87 0.0161693 979
│ 4 │ 12 TOPIC_124 TOPIC_87 0.00660719 979
│ 5 │ 12 TOPIC_140 TOPIC_87 0.000891694 979
│ 6 │ 12 TOPIC_101 TOPIC_87 0.00134154 979
│ 7 │ 12 TOPIC_89 TOPIC_87 0.0784224 979
│ 8 │ 12 TOPIC_233 TOPIC_87 0.0195678 979
│ 9 │ 12 TOPIC_144 TOPIC_87 0.0150135 979
└ 10 │ 12 TOPIC_201 TOPIC_87 0.00740799 979
[ Info: The dimensions for the pij_df: (16812500, 5)

```

I am trying to generate a new column “PROB\_I\_J” from the columns “JOINT\_PROB\_SUM” and “DOC\_COUNT\_SUM”. Each row in “PROB\_I\_J” should be the value for “JOINT\_PROB\_SUM” divided by the “DOC\_COUNT\_SUM”.

I am using the following DataFramesMeta macro:

```julia
@transform!(pij_df, :PROB_I_J = :JOINT_PROB_SUM / :DOC_COUNT_SUM)

```

I am receiving an OutOfMemoryError() from running the transform macro. I know the dataframe is quite large, but I am suspecting there is something wrong with the transform macro. Is the code above doing what I want it to do? I’ve generally used dataframes and dataframesmeta to manipulate data but I’m wondering if this is less efficient…

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [February 22, 2022, 8:26pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931/2 "2022-02-22T20:26:12Z")

</div>

> [@a.frist](#):
>
> `@transform!(pij_df, :PROB_I_J = :JOINT_PROB_SUM / :DOC_COUNT_SUM)`

do

```julia
@rtransform!(pij_df, :PROB_I_J = :JOINT_PROB_SUM / :DOC_COUNT_SUM)

```

or

```julia
@transform!(pij_df, :PROB_I_J = :JOINT_PROB_SUM ./ :DOC_COUNT_SUM)

```

---

<div class="post-metadata">

### Author: ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)
#### Post date: [February 22, 2022, 8:48pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931/3 "2022-02-22T20:48:21Z")

</div>

This works! @bkamins can you elaborate on what my original code was doing? I’ve reviewed the docs a few times and it isn’t clear to me what happens in the original transform. Is performing the calculation by mapping the first element in column1 with every single element in column2?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 23, 2022, 5:52am UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931/4 "2022-02-23T05:52:43Z")

</div>

Without any macros:

```julia
pij_df.PROB_I_J = pij_df.JOINT_PROB_SUM ./ pij_df.DOC_COUNT

```

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [February 23, 2022, 7:44am UTC](https://discourse.julialang.org/t/how-do-i-create-a-new-dataframe-column-by-dividing-individual-elements-in-two-dataframe-columns/76931/5 "2022-02-23T07:44:15Z")

</div>

Your original code is creating a matrix, e.g.:

```julia
julia> [1, 2, 3] / [4, 5, 6]
3×3 Matrix{Float64}:
 0.0519481 0.0649351 0.0779221
 0.103896 0.12987 0.155844
 0.155844 0.194805 0.233766

```

which cannot be stored in a column of a data frame.
