Is it possible to rename columns in a DataFrame view ?
(without altering the column names in the underlying DataFrame)
Is it possible to rename columns in a DataFrame view ?
(without altering the column names in the underlying DataFrame)
For a single column this does the job: select(df, :A => :B, copycols=false)
.
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
df = DataFrame(A=1:2,B=3:4)
x = select( df, :A => :a, :B => :b, copycols=false)
df[1,:A] = 99
x
julia> x
2Γ2 DataFrame
Row β a b
β Int64 Int64
ββββββΌββββββββββββββ
1 β 99 3
2 β 2 4
Though I notice that changing the original does change the select() object
Yes, because the columns arenβt copied.
From the documentation:
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;
Thechnically this is not a view, but as @chiraganand commented - it just reuses the columns.