Extracting Ints from a String in a DataFrame

I have a DataFrame like below:

df = DataFrame("Code" => ["A03", "W12","C04"])

The df.Code column is always a 3-character string formed by 1 letter and 2 digits. I need to extract the digits (as int) to a new column lets say Code_Int. So “A03” will be 3, “W12” will be 12 and so on. This column has 180000 records with this exact pattern.

I have no clue how to do it. I tried getindex.(string(df.Code), Ref(1:3)) but didnt. work. Any help?
Thanks

Not the whole thing, but parse(Int64, x[2:end]) would get an Int64 value from a String value omitting the first character.

i create a new column insertcols!(df,4,:seed_int=>0) then your suggestion as df[:seed_int]=[parse(Int64,x) for x in df[!,:Code] and i get "ArgumentError: Invalid base 10 digit ‘W’ in “W10”
btw: “W10” is the first code in the original df.

Side note, you are using a very old version of DataFrames if df[:var] works. Consider updating.

I would use regex to replace all non-numeric characters


julia> df.num = parse.(Int, replace.(df.Code, r"[A-Z]" => ""))
3-element Array{Int64,1}:
  3
 12
  4
3 Likes

Nice! your code worked. I am using version 0.22.5. Thanks man!