# Named Capture Group to create new columns

**URL:** <https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [August 7, 2021, 7:48pm UTC](https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997 "2021-08-07T19:48:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![magrathean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magrathean/32/20389_2.png) [@magrathean](https://discourse.julialang.org/u/magrathean)\
**Post date:** [August 7, 2021, 7:48pm UTC](https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997/1 "2021-08-07T19:48:01Z")

</div>

How do I create new columns from matches from a named capture group?

```julia
df = DataFrame(time = ["twelve:nine", "one:five"])
rx = r"(?<hour>[a-z]*):(?<minute>[a-z]*)"

```

```julia
2×1 DataFrame
 Row │ time        
     │ String      
─────┼─────────────
   1 │ twelve:nine
   2 │ one:three

```

I want to create:

```julia

2×2 DataFrame
 Row │ hour minute        
     │ String String
─────┼─────────────
   1 │ twelve nine
   2 │ one three

```

Something like the following does not work:

```julia
select(df, :time => ByRow(x -> match(rx, x).captures) => AsTable)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 7, 2021, 11:21pm UTC](https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997/2 "2021-08-07T23:21:32Z")

</div>

Couldn’t you rename the dataframe and optionally convert the SubStrings to Strings?

```julia
df2 = select(df, :time => ByRow(x -> match(rx, x).captures) => AsTable)
rename!(df2, [:hour, :minute])
df2[!,1:2] = convert.(String, df2[!,1:2])

2×2 DataFrame
 Row │ hour minute 
     │ String String 
─────┼────────────────
   1 │ twelve nine
   2 │ one five

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 8, 2021, 12:23am UTC](https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997/3 "2021-08-08T00:23:55Z")

</div>

Here is the code to create a Tables.jl table (compatible with DataFrames)  
With columns based on regex named captures.

> [@Creating a Table from Regex](https://discourse.julialang.org/t/creating-a-table-from-regex/63201):
>
> It occurred to me that it would be nice to be able to create a table from a string by providing a regex that uses named groups. nushell’s parse function ([docs](https://www.nushell.sh/book/commands/parse.html), [example](https://www.nushell.sh/cookbook/parsing.html)) does this. A RegexMatch has all the right things for a Tables.jl row, except it doesn’t provide getproperty overloads as historically it’s fields were part of it’s public API (though that has progressively become less true, it would be too breaking to change that now). But we can wrap a RegexMatch in a suitable type. (Might a…

Not sure if that is useful to you.

---

<div class="post-metadata">

**Author:** ![magrathean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magrathean/32/20389_2.png) [@magrathean](https://discourse.julialang.org/u/magrathean)\
**Post date:** [August 8, 2021, 2:50am UTC](https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997/4 "2021-08-08T02:50:25Z")

</div>

Yes, renaming works. If I did use the named captures, renaming seems redundant.

---

<div class="post-metadata">

**Author:** ![magrathean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magrathean/32/20389_2.png) [@magrathean](https://discourse.julialang.org/u/magrathean)\
**Post date:** [August 8, 2021, 2:50am UTC](https://discourse.julialang.org/t/named-capture-group-to-create-new-columns/65997/5 "2021-08-08T02:50:52Z")

</div>

Thanks. Will check that out.
