Converting column type from String to Int in a DataFrame in Julia

dataframe, julia

Solution

`parse()` should work:

df = DataFrame(
  yearsAsString = ["2016", "2017", "2018"] 
)
df[:years] = [parse(Int,x) for x in df[:yearsAsString]] 

df

3×2 DataFrames.DataFrame
│ Row │ yearsAsString │ years │
├─────┼───────────────┼───────┤
│ 1   │ "2016"        │ 2016  │
│ 2   │ "2017"        │ 2017  │
│ 3   │ "2018"        │ 2018  │

(remember to use `Int` with the capital I)

Edited (thanks MattB)

In current Julia 0.5.1 (and 0.6 alpha) this works too:

df[:y2] = parse.([Int],df[:yearsAsString])

In Julia 0.6 only:

df[:y2] = parse.(Int,df[:yearsAsString])

Problem

I want to change column datatype in Julia from string to int but no luck so far. Neither `convert()` nor `parse()` works. Is there any way to do that? I have tried these but none works `df[:serial] = int.(collect(df[:strSerial])) df[:serial] = map(x->parse(Int,x),df[:strSerial]) df[:serial] = Int64(df[:strSerial])`

Original source