# Extract field from the elements of a column in a DataFrame

**URL:** <https://discourse.julialang.org/t/extract-field-from-the-elements-of-a-column-in-a-dataframe/47405>\
**Category:** Data\
**Created:** [September 28, 2020, 1:32pm UTC](https://discourse.julialang.org/t/extract-field-from-the-elements-of-a-column-in-a-dataframe/47405 "2020-09-28T13:32:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [September 28, 2020, 1:32pm UTC](https://discourse.julialang.org/t/extract-field-from-the-elements-of-a-column-in-a-dataframe/47405/1 "2020-09-28T13:32:16Z")

</div>

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

```julia
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:

```julia
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?**

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 28, 2020, 1:49pm UTC](https://discourse.julialang.org/t/extract-field-from-the-elements-of-a-column-in-a-dataframe/47405/2 "2020-09-28T13:49:22Z")

</div>

You need `^(:a)` to tell `@transform` to treat that `:a` as a `Symbol` and not a vector in the DataFrame. See the docs [here](https://github.com/JuliaData/DataFramesMeta.jl#with).

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [September 28, 2020, 2:16pm UTC](https://discourse.julialang.org/t/extract-field-from-the-elements-of-a-column-in-a-dataframe/47405/3 "2020-09-28T14:16:37Z")

</div>

> [@Gregstrq](#):
>
> `getfield.(:C1, :a)`

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`.

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [September 28, 2020, 4:58pm UTC](https://discourse.julialang.org/t/extract-field-from-the-elements-of-a-column-in-a-dataframe/47405/4 "2020-09-28T16:58:34Z")

</div>

Thank you. It helped.
