# Extracting Ints from a String in a DataFrame

**URL:** https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293
**Category:** General Usage
**Tags:** dataframes
**Created:** [March 1, 2021, 11:54pm UTC](https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293 "2021-03-01T23:54:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)
#### Post date: [March 1, 2021, 11:54pm UTC](https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293/1 "2021-03-01T23:54:03Z")

</div>

I have a DataFrame like below:

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

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [March 2, 2021, 12:04am UTC](https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293/2 "2021-03-02T00:04:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)
#### Post date: [March 2, 2021, 12:21am UTC](https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293/3 "2021-03-02T00:21:16Z")

</div>

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.

---

<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: [March 2, 2021, 12:30am UTC](https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293/4 "2021-03-02T00:30:46Z")

</div>

> [@statspy](#):
>
> df[:seed\_int]

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

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

```

---

<div class="post-metadata">

### Author: ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)
#### Post date: [March 2, 2021, 12:47am UTC](https://discourse.julialang.org/t/extracting-ints-from-a-string-in-a-dataframe/56293/5 "2021-03-02T00:47:59Z")

</div>

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