How do I create new columns from matches from a named capture group?
df = DataFrame(time = ["twelve:nine", "one:five"])
rx = r"(?<hour>[a-z]*):(?<minute>[a-z]*)"
2×1 DataFrame
Row │ time
│ String
─────┼─────────────
1 │ twelve:nine
2 │ one:three
I want to create:
2×2 DataFrame
Row │ hour minute
│ String String
─────┼─────────────
1 │ twelve nine
2 │ one three
Something like the following does not work:
select(df, :time => ByRow(x -> match(rx, x).captures) => AsTable)
Couldn’t you rename the dataframe and optionally convert the SubStrings to Strings?
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
Here is the code to create a Tables.jl table (compatible with DataFrames)
With columns based on regex named captures.
Not sure if that is useful to you.
Yes, renaming works. If I did use the named captures, renaming seems redundant.
Thanks. Will check that out.