# New Dataframe Column from a View from Another DataFrames

**URL:** <https://discourse.julialang.org/t/new-dataframe-column-from-a-view-from-another-dataframes/53945>\
**Category:** General Usage\
**Tags:** data, dataframes\
**Created:** [January 26, 2021, 12:13am UTC](https://discourse.julialang.org/t/new-dataframe-column-from-a-view-from-another-dataframes/53945 "2021-01-26T00:13:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [January 26, 2021, 12:13am UTC](https://discourse.julialang.org/t/new-dataframe-column-from-a-view-from-another-dataframes/53945/1 "2021-01-26T00:13:23Z")

</div>

Hi all,

I have a dataframe and a column view of a another dataframe `B` like this:

```julia
A = DataFrame([1 3 5; 1 3 5; 2 4 6; 2 4 6;])
C = @view B[col]

```

`A` looks like this:

```julia
4×3 DataFrame
 Row │ x1 x2 x3
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 3 5
   2 │ 1 3 5
   3 │ 2 4 6
   4 │ 2 4 6

```

`C` looks like this:

```julia
[7, 8]

```

What I want to create is something that looks like this from `C`:

```julia
4×4 DataFrame
 Row │ x1 x2 x3 x4
     │ Int64 Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 3 5 7
   2 │ 1 3 5 7
   3 │ 2 4 6 8
   4 │ 2 4 6 8

```

Where `x4` is constructed entirely from viewed values. Is this possible to do? And if so, how?

Thanks!

~tcp 🌳

---

<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:** [January 26, 2021, 1:29am UTC](https://discourse.julialang.org/t/new-dataframe-column-from-a-view-from-another-dataframes/53945/2 "2021-01-26T01:29:24Z")

</div>

> [@TheCedarPrince](#):
>
> ```julia
> A = DataFrame([1 3 5; 1 3 5; 2 4 6; 2 4 6;])
> C = @view B[col]
> 
> ```

First, note that the fact that this works means you aren’t on the newest version of DataFrames (assuming `B` is a DataFrame)

It looks like you can do this, with `!`

```julia
julia> using DataFrames

julia> df = DataFrame();

julia> x = [1, 2, 3, 4];

julia> y = @view x[1:2]
2-element view(::Array{Int64,1}, 1:2) with eltype Int64:
 1
 2

julia> df[!, :a] = y;

julia> df.a
2-element view(::Array{Int64,1}, 1:2) with eltype Int64:
 1
 2

```
