Skip to content

Add Before for column selection #3491

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
─────┼───────────────────────────────────
   1 │     1      5      2      3      4
   2 │     2      7      3      4      1
   3 │     3      5      4      1      2
   4 │     4      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
─────┼───────────────────────────────────
   1 │     1      5      2      3      4
   2 │     2      7      3      4      1
   3 │     3      5      4      1      2
   4 │     4      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
─────┼───────────────────────────────────
   1 │     1      2      3      5      4
   2 │     2      3      4      7      1
   3 │     3      4      1      5      2
   4 │     4      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
─────┼───────────────────────────────────
   1 │     1      2      3      5      4
   2 │     2      3      4      7      1
   3 │     3      4      1      5      2
   4 │     4      1      2      3      3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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