# Transforming string columns in DataFrame with (regex) match

**URL:** <https://discourse.julialang.org/t/transforming-string-columns-in-dataframe-with-regex-match/70395>\
**Category:** General Usage\
**Created:** [October 26, 2021, 10:09am UTC](https://discourse.julialang.org/t/transforming-string-columns-in-dataframe-with-regex-match/70395 "2021-10-26T10:09:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [October 26, 2021, 10:09am UTC](https://discourse.julialang.org/t/transforming-string-columns-in-dataframe-with-regex-match/70395/1 "2021-10-26T10:09:20Z")

</div>

```julia
using DataFrames
using DataFramesMeta

# This is the data
df = DataFrame(A = 1:5, B = ["Th32is","i35s","124my","test775","var23ia5ble"])

# This is what the output should be
@transform!(df, :B = match.(r"\d+", :B))
@transform(df, :B = [x.match for x in :B])

# But I would like to do it in one transform call

# I tried to just use getproperty
df = DataFrame(A = 1:5, B = ["Th32is","i35s","124my","test775","var23ia5ble"])
@transform(df, :B = match.(r"\d+", :B).match)

# trying to broadcast explicitly
df = DataFrame(A = 1:5, B = ["Th32is","i35s","124my","test775","var23ia5ble"])
@transform(df, :B = getproperty.(match.(r"\d+", :B), :match))

# but neither of these work

```

Is there a (not too convoluted way) to do this in one transform call?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2021, 10:38am UTC](https://discourse.julialang.org/t/transforming-string-columns-in-dataframe-with-regex-match/70395/2 "2021-10-26T10:38:11Z")

</div>

If DataFramesMeta and `transform!` aren’t absolutely necessary, you could just do:

```julia
julia> df = DataFrame(A = 1:5, B = ["Th32is","i35s","124my","test775","var23ia5ble"])
5×2 DataFrame
 Row │ A B
     │ Int64 String      
─────┼────────────────────
   1 │ 1 Th32is
   2 │ 2 i35s
   3 │ 3 124my
   4 │ 4 test775
   5 │ 5 var23ia5ble

julia> df.B = [x.match for x in match.(r"\d+", df.B)]; df
5×2 DataFrame
 Row │ A B
     │ Int64 SubStrin… 
─────┼──────────────────
   1 │ 1 32        
   2 │ 2 35        
   3 │ 3 124
   4 │ 4 775
   5 │ 5 23

```

---

<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:** [October 26, 2021, 11:27am UTC](https://discourse.julialang.org/t/transforming-string-columns-in-dataframe-with-regex-match/70395/3 "2021-10-26T11:27:59Z")

</div>

Use `@rtransform`, the row-wise version of `@transform`.

```julia
julia> @rtransform(df, :B = match(r"\d+", :B).match)
5×2 DataFrame
 Row │ A B
     │ Int64 SubStrin…
─────┼──────────────────
   1 │ 1 32
   2 │ 2 35
   3 │ 3 124
   4 │ 4 775
   5 │ 5 23

```

As for your problem with `getproperty`, in `@transform`, which accepts full columns, check out the docs [here](https://juliadata.github.io/DataFramesMeta.jl/stable/#Working-with-Symbols-without-referring-to-columns) about “Working with `Symbol` s without referring to columns”. You need to escape the `Symbol` with `^`.

```julia
julia> @transform(df, :B = getproperty.(match.(r"\d+", :B), ^(:match)))
5×2 DataFrame
 Row │ A B
     │ Int64 SubStrin…
─────┼──────────────────
   1 │ 1 32
   2 │ 2 35
   3 │ 3 124
   4 │ 4 775
   5 │ 5 23

```

---

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [October 26, 2021, 12:09pm UTC](https://discourse.julialang.org/t/transforming-string-columns-in-dataframe-with-regex-match/70395/4 "2021-10-26T12:09:20Z")

</div>

Thanks for both of these excellent answers, much appreciated! 🙂
