Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Before for column selection #3491

Open
mathieu17g opened this issue Feb 19, 2025 · 0 comments
Open

Add Before for column selection #3491

mathieu17g opened this issue Feb 19, 2025 · 0 comments

Comments

@mathieu17g
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant