I try to find a simple way to convert a column with type string to float.
I tried some:
df[!,x] = parse(Float64, df[!,y])
but it doesn't work.
I would be grateful if you have any idea Thks
I try to find a simple way to convert a column with type string to float.
I tried some:
df[!,x] = parse(Float64, df[!,y])
but it doesn't work.
I would be grateful if you have any idea Thks
You need to broadcast it (note the . after parse):
julia> df = DataFrame(y = ["1", "2"])
2×1 DataFrame
│ Row │ y │
│ │ String │
├─────┼────────┤
│ 1 │ 1 │
│ 2 │ 2 │
julia> df.x = parse.(Float64, df.y)
2-element Array{Float64,1}:
1.0
2.0
julia> df
2×2 DataFrame
│ Row │ y │ x │
│ │ String │ Float64 │
├─────┼────────┼─────────┤
│ 1 │ 1 │ 1.0 │
│ 2 │ 2 │ 2.0 │
an alternative using transform! would be (it is more verbose in this case):
julia> transform!(df, :y => ByRow(x -> parse(Float64, x)) => :x)
2×2 DataFrame
│ Row │ y │ x │
│ │ String │ Float64 │
├─────┼────────┼─────────┤
│ 1 │ 1 │ 1.0 │
│ 2 │ 2 │ 2.0 │
If you have missing values in :y replace parse by passmissing(parse) in the codes above, e.g.
df.x = passmissing(parse).(Float64, df.y)