Deconstruct NamedTupleColumn with Prefix

Similar to this one: Transform! to destructure NamedTuple into columns

I’d like to do the same, but be able to programmatically prefix the "child"columns with the “parent” prefix.

That is in the linked MWE, I would like to get column names x_a and x_b (automatically)

Something like

nms = df.x[1] |> keys .|> string
rename!(df, (nms .=> "x" .* nms)...)

works but feels cumbersome. Can it be done directly in the transform statement?

Thanks!

Nothing super easy in DataFrames.jl for this, unfortunately. You could do

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

julia> df = DataFrame(x = rand(2), y =[(a=1,b=2),(a=3,b=5)])
2×2 DataFrame
 Row │ x         y
     │ Float64   NamedTup…
─────┼──────────────────────────
   1 │ 0.485175  (a = 1, b = 2)
   2 │ 0.822109  (a = 3, b = 5)

julia> transform(df, :y => ByRow(t -> add_prefix(t, "y")) => AsTable)
2×4 DataFrame
 Row │ x         y               y_a    y_b
     │ Float64   NamedTup…       Int64  Int64
─────┼────────────────────────────────────────
   1 │ 0.485175  (a = 1, b = 2)      1      2
   2 │ 0.822109  (a = 3, b = 5)      3      5

But that doesn’t give you the name “y” automatically. If you really want access to column names inside the fun of src => fun => dest you could do AsTable(src) => ... but that’s probably more trouble than its worth.

1 Like