Extract field from the elements of a column in a DataFrame

Let’s say I a have a DataFrame with a column :C1. The elements of the column are structs of the type

struct MyStruct
    a::Float64
    b::Float64
end

How do I extract, for example, the field :a from the elements of the column :C1 into a separate column?
I am trying to do it with the following command:

new_df = @linq df |> transform(a = getfield.(:C1, :a))

However, it seems, that getfield is dispatched on the whole column :C1 instead of the elements of this column, so the command does not work.

Does anyone have the ideas how to fix the wrong dispatch? May be I should use some other approach?

You need ^(:a) to tell @transform to treat that :a as a Symbol and not a vector in the DataFrame. See the docs here.

1 Like

FWIW, getproperty.(vec, :a) tends to have poor performance because the :a constant isn’t propagated across the broadcasting machinery. I assume it’s the same for getfield.

Thank you. It helped.