# Replace data from specific column in dataframe

**URL:** <https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [April 19, 2024, 4:01pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248 "2024-04-19T16:01:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandy45](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sandy45](https://discourse.julialang.org/u/Sandy45)\
**Post date:** [April 19, 2024, 4:01pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248/1 "2024-04-19T16:01:41Z")

</div>

I have two dataframes as shown below and need to replace one column(df.M02) with column(df2.M02) for year 2024 only. I know this can be achieved in multiple ways but I am just trying to find better way to do this.

```julia
2×13 DataFrame
 Row │ name type year sub_type M01 M02 M03 M04 M05 M06 M07 M08 M09     
     │ String String Int64 String Float64? Float64 Float64 Float64 Float64 Float64 Float64? Float64? Float64 
─────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ test-24 nulltest 2024 prev_mth missing 3.4 5.6 7.0 8.0 9.0 missing missing 8.5
   2 │ test-24 nulltest 2025 prev_mth missing 3.4 5.6 7.0 8.0 9.0 missing missing 8.5

```

```julia
2×13 DataFrame
 Row │ name type year sub_type M01 M02 M03 M04 M05 M06 M07 M08 M09     
     │ String String Int64 String Float64? Float64 Float64 Float64 Float64 Float64 Float64? Float64? Float64 
─────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ test-23 nulltest 2024 prev_mth missing 3.4 5.6 7.0 8.0 9.0 missing missing 8.5
   2 │ test-24 nulltest 2025 prev_mth missing 3.4 5.6 7.0 8.0 9.0 missing missing 8.5

```

---

<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:** [April 19, 2024, 4:03pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248/2 "2024-04-19T16:03:20Z")

</div>

I would say:

```julia
mask1 = df.year .== 2024
mask2 = df2.year .== 2024

@assert mask1 == mask2

@views @. df.M02[mask1] = df2.M02[mask2]

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [April 19, 2024, 4:19pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248/3 "2024-04-19T16:19:52Z")

</div>

If you know that the rows are identical and in order:

```julia
df.M02 = ifelse.(df.year .== 2024, df2.M02, df.M02)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 19, 2024, 8:25pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248/4 "2024-04-19T20:25:40Z")

</div>

```julia
using CSV, DataFrames

df1="""
 name type year sub_type M01 M02 M03 M04 M05 M06 M07 M08 M09     
 test-24 nulltest 2024 prev_mth missing 3.41 5.6 7.0 8.0 9.0 missing missing 8.5
 test-24 nulltest 2025 prev_mth missing 3.41 5.6 7.0 8.0 9.0 missing missing 8.5
 """

df1=CSV.read(IOBuffer(df1), DataFrame, delim=' ', ignorerepeated=true)

df2="""
 name type year sub_type M01 M02 M03 M04 M05 M06 M07 M08 M09     
 test-23 nulltest 2024 prev_mth missing 3.42 5.6 7.0 8.0 9.0 missing missing 8.5
 test-24 nulltest 2025 prev_mth missing 3.42 5.6 7.0 8.0 9.0 missing missing 8.5
 """

 
df2=CSV.read(IOBuffer(df2), DataFrame, delim=' ', ignorerepeated=true)

grp1=groupby(df1,:year)

grp2=groupby(df2,:year)

# (df.M02) with column(df2.M02) 

grp1[(2024,)].M02=grp2[(2024,)].M02

julia> df1
2×13 DataFrame
 Row │ name type year sub_type M01 M02 M03 M04 M05 M06 M07 M08 M0 ⋯
     │ String7 String15 Int64 String15 String7 Float64 Float64 Float64 Float64 Float64 String7 String7 Fl ⋯
─────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ test-24 nulltest 2024 prev_mth missing 3.42 5.6 7.0 8.0 9.0 missing missing ⋯
   2 │ test-24 nulltest 2025 prev_mth missing 3.41 5.6 7.0 8.0 9.0 missing missing
                                                                                                       1 column omitted

```

---

<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:** [April 19, 2024, 8:41pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248/5 "2024-04-19T20:41:32Z")

</div>

A `leftjoin` is probably the best choice here. Unless you _absolutely know_ that the rows in each data frame overlap, you should avoid doing stuff like `df1.x = df2.x`.

Here’s a solution involving a `join` and DataFramesMeta.jl

```julia
@chain df begin 
    leftjoin(df1, @select(df2, :id, :M02_2 = :M02), on = :id, )
    @rtransform :M02 = :year == 2004 ? :M02_2 : M02
end

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 20, 2024, 1:52pm UTC](https://discourse.julialang.org/t/replace-data-from-specific-column-in-dataframe/113248/6 "2024-04-20T13:52:29Z")

</div>

> [@pdeffebach](#):
>
> Unless you _absolutely know_ that the rows in each data frame overlap,

despite all attempts to give an answer, the question seems to lie precisely on this aspect: the problem is not well defined.
