# Rename columns in view

**URL:** <https://discourse.julialang.org/t/rename-columns-in-view/85272>\
**Category:** General Usage\
**Tags:** dataframes, views\
**Created:** [August 4, 2022, 12:09am UTC](https://discourse.julialang.org/t/rename-columns-in-view/85272 "2022-08-04T00:09:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [August 4, 2022, 12:09am UTC](https://discourse.julialang.org/t/rename-columns-in-view/85272/1 "2022-08-04T00:09:17Z")

</div>

Is it possible to rename columns in a DataFrame view ?

(without altering the column names in the underlying DataFrame)

---

<div class="post-metadata">

**Author:** ![chiraganand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiraganand/32/32787_2.png) [@chiraganand](https://discourse.julialang.org/u/chiraganand)\
**Post date:** [August 4, 2022, 5:43am UTC](https://discourse.julialang.org/t/rename-columns-in-view/85272/2 "2022-08-04T05:43:58Z")

</div>

For a single column this does the job: `select(df, :A => :B, copycols=false)`.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [August 4, 2022, 6:08am UTC](https://discourse.julialang.org/t/rename-columns-in-view/85272/3 "2022-08-04T06:08:21Z")

</div>

Thanks. this seems to work for multiple columns as well.

I though that a view had to be a SubDataFrame.

Since `select()` returns a `DataFrame` I thought it was creating a new object.  
Though I notice that changing the original does change the `select()` object

```julia
df = DataFrame(A=1:2,B=3:4)
x = select( df, :A => :a, :B => :b, copycols=false)
df[1,:A] = 99
x

```

```julia
julia> x
2×2 DataFrame       
 Row │ a b     
     │ Int64 Int64 
─────┼──────────────
   1 │ 99 3 
   2 │ 2 4 

```

---

<div class="post-metadata">

**Author:** ![chiraganand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiraganand/32/32787_2.png) [@chiraganand](https://discourse.julialang.org/u/chiraganand)\
**Post date:** [August 4, 2022, 6:12am UTC](https://discourse.julialang.org/t/rename-columns-in-view/85272/4 "2022-08-04T06:12:46Z")

</div>

> Though I notice that changing the original does change the select() object

Yes, because the columns aren’t copied.

From the [documentation](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.select):

> In general columns returned by transformations are stored in the target data frame without copying. An exception to this rule is when columns from the source data frame are reused in the target data frame. This can happen via expressions like: :x1, [:x1, :x2], :x1 =\> :x2, :x1 =\> identity =\> :x2, or :x1 =\> (x → @view x[inds]) (note that in the last case the source column is reused indirectly via a view). In such cases the behavior depends on the value of the copycols keyword argument:
> 
> - if copycols=true then results of such transformations always perform a copy of the source column or its view;
> - if copycols=false then copies are only performed to avoid storing the same column several times in the target data frame; more precisely, no copy is made the first time a column is used, but each subsequent reuse of a source column (when compared using ===, which excludes views of source columns) performs a copy;

---

<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:** [August 4, 2022, 6:37am UTC](https://discourse.julialang.org/t/rename-columns-in-view/85272/5 "2022-08-04T06:37:04Z")

</div>

Thechnically this is not a view, but as @chiraganand commented - it just reuses the columns.
