Skip to content

Add Before for column selection #3491

@mathieu17g

Description

@mathieu17g

Wouldn't it be great to add the Before selector to Not, Between, Cols, All and regex column selectors?

This would simplify a bit the use case where you add a new column with transform and move it to a specific position, for instance around the column used for the transformation.

Example:

julia> df = DataFrame(:a => [1, 2, 3, 4], :b1 => [2, 3, 4, 1], :B2 => [3, 4, 1, 2], :c => [4, 1, 2, 3])       
       transform!(df, [:b1, :B2] => ByRow(+) => :b)
       select!(df, Cols(1:findfirst(==("b1"), names(df))-1, :b, :))
4×5 DataFrame
 Row │ a      b      b1     B2     c     
     │ Int64  Int64  Int64  Int64  Int64
─────┼───────────────────────────────────
   11      5      2      3      4
   22      7      3      4      1
   33      5      4      1      2
   44      3      1      2      3

julia> 

The reordering would be

julia> df = DataFrame(:a => [1, 2, 3, 4], :b1 => [2, 3, 4, 1], :B2 => [3, 4, 1, 2], :c => [4, 1, 2, 3])       
       transform!(df, [:b1, :B2] => ByRow(+) => :b)
       select!(df, Cols(Before(:b1), :b, :))
4×5 DataFrame
 Row │ a      b      b1     B2     c     
     │ Int64  Int64  Int64  Int64  Int64
─────┼───────────────────────────────────
   11      5      2      3      4
   22      7      3      4      1
   33      5      4      1      2
   44      3      1      2      3

After could also be useful

julia> df = DataFrame(:a => [1, 2, 3, 4], :b1 => [2, 3, 4, 1], :B2 => [3, 4, 1, 2], :c => [4, 1, 2, 3])       
       transform!(df, [:b1, :B2] => ByRow(+) => :b)
       select!(df, Cols(1:findfirst(==("B2"), names(df)), :b, :))
4×5 DataFrame
 Row │ a      b1     B2     b      c     
     │ Int64  Int64  Int64  Int64  Int64
─────┼───────────────────────────────────
   11      2      3      5      4
   22      3      4      7      1
   33      4      1      5      2
   44      1      2      3      3

Would become

julia> df = DataFrame(:a => [1, 2, 3, 4], :b1 => [2, 3, 4, 1], :B2 => [3, 4, 1, 2], :c => [4, 1, 2, 3])       
       transform!(df, [:b1, :B2] => ByRow(+) => :b)
       select!(df, Cols(Not(After(:B2)), :b, :))
4×5 DataFrame
 Row │ a      b1     B2     b      c     
     │ Int64  Int64  Int64  Int64  Int64
─────┼───────────────────────────────────
   11      2      3      5      4
   22      3      4      7      1
   33      4      1      5      2
   44      1      2      3      3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions