# How to copy a dataframe column within the same dataframe?

**URL:** <https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [May 19, 2025, 9:20am UTC](https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136 "2025-05-19T09:20:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 19, 2025, 9:20am UTC](https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136/1 "2025-05-19T09:20:43Z")

</div>

Hi,

I would like to copy (not a view) a column of dataframe inside the same dataframe.

E.g. to have:

```julia
julia> df = DataFrame(A=1:4, B=["A", "B", "C", "D"])
4×2 DataFrame
 Row │ A B      
     │ Int64 String 
─────┼───────────────
   1 │ 1 A
   2 │ 2 B
   3 │ 3 C
   4 │ 4 D

```

into, let’s say the “:new\_col” containing the same elements as “:A”.  
I’ve tried using

```julia
insertcols!(df, 3, copy(df.A), :new_col)

```

and some variants without success…

---

<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:** [May 19, 2025, 9:29am UTC](https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136/2 "2025-05-19T09:29:19Z")

</div>

Do you mean this?

```julia
julia> df = DataFrame(A=1:4, B=["A", "B", "C", "D"])
4×2 DataFrame
 Row │ A B
     │ Int64 String
─────┼───────────────
   1 │ 1 A
   2 │ 2 B
   3 │ 3 C
   4 │ 4 D

julia> df.b_again = copy(df.B);

julia> df.b_again[2] = "E";

julia> df
4×3 DataFrame
 Row │ A B b_again
     │ Int64 String String
─────┼────────────────────────
   1 │ 1 A A
   2 │ 2 B E
   3 │ 3 C C
   4 │ 4 D D

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 19, 2025, 9:32am UTC](https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136/3 "2025-05-19T09:32:59Z")

</div>

Wonderful !  
Thanks !

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 19, 2025, 9:35am UTC](https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136/4 "2025-05-19T09:35:48Z")

</div>

> [@Ju\_ska](#):
>
> `insertcols!(df, 3, copy(df.A), :new_col)`

You could write:

```julia
insertcols!(df, 3, :new_col => df.A)

```

which in this case is the same as:

```julia
insertcols!(df, :new_col => df.A)

```

---

<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:** [May 19, 2025, 9:44am UTC](https://discourse.julialang.org/t/how-to-copy-a-dataframe-column-within-the-same-dataframe/129136/5 "2025-05-19T09:44:14Z")

</div>

Also consider using the following syntaxes:

```julia
julia> df = DataFrame(A=1:4, B=["A", "B", "C", "D"])
4×2 DataFrame
 Row │ A B      
     │ Int64 String 
─────┼───────────────
   1 │ 1 A
   2 │ 2 B
   3 │ 3 C
   4 │ 4 D

julia> df[!, :C] = df.A
4-element Vector{Int64}:
 1
 2
 3
 4

julia> df
4×3 DataFrame
 Row │ A B C     
     │ Int64 String Int64 
─────┼──────────────────────
   1 │ 1 A 1
   2 │ 2 B 2
   3 │ 3 C 3
   4 │ 4 D 4

julia> df.C[3]
3

julia> df.C[3] = 33
33

julia> df
4×3 DataFrame
 Row │ A B C     
     │ Int64 String Int64
─────┼──────────────────────
   1 │ 1 A 1
   2 │ 2 B 2
   3 │ 33 C 33
   4 │ 4 D 4

julia> df[:, :D] = df.A
4-element Vector{Int64}:
  1
  2
 33
  4

julia> df.D[3]=3
3

julia> df
4×4 DataFrame
 Row │ A B C D     
     │ Int64 String Int64 Int64
─────┼─────────────────────────────
   1 │ 1 A 1 1
   2 │ 2 B 2 2
   3 │ 33 C 33 3
   4 │ 4 D 4 4

```
