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

Request - $AsTable with prefix #398

Closed
Lincoln-Hannah opened this issue Jun 13, 2024 · 2 comments
Closed

Request - $AsTable with prefix #398

Lincoln-Hannah opened this issue Jun 13, 2024 · 2 comments

Comments

@Lincoln-Hannah
Copy link

Lincoln-Hannah commented Jun 13, 2024

DF = DataFrame(  A= (a=1,b=2,c=3))

@chain begin
    @rtransform DF $AsTable = :A
    rename!(_,  "X_" .* names(_) )
end

To condense 2 lines into 1, could there be a syntax like:

@rtransform DF $AsTable(prefix="X_") = :A

Use Case
Sometimes multiple columns contain named tuples with the same field names. I'd like to expand them all into the main table with different prefixes.

@Lincoln-Hannah Lincoln-Hannah changed the title Request $AsTable with prefix Request - $AsTable with prefix Jun 13, 2024
@pdeffebach
Copy link
Collaborator

I think this is better handled via a helper-function, to be honest.

But I appreciate your feedback and hope to work on other of your suggestions soon.

julia> function prefix_nt(pre, nt)
           nms = Symbol.(pre, "_", propertynames(nt))
           NamedTuple{nms}(values(nt))
       end
prefix_nt (generic function with 1 method)

julia> @rtransform DF $AsTable = prefix_nt("x", :A)
1×4 DataFrame
 Row │ A                      x_a    x_b    x_c   
     │ NamedTup…              Int64  Int64  Int64 
─────┼────────────────────────────────────────────
   1 │ (a = 1, b = 2, c = 3)      1      2      3

@Lincoln-Hannah
Copy link
Author

Thanks Peter. that works well :)

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

2 participants