# How to parse/convert integers in DataFrame to float numbers

**URL:** https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507
**Category:** New to Julia
**Tags:** dataframes
**Created:** [March 19, 2021, 12:07am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507 "2021-03-19T00:07:05Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 12:07am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/1 "2021-03-19T00:07:05Z")

</div>

Hi.

I am a beginner in Julia. I am trying to get data from a single row and perform statistics on it like taking the mean, median, etc. However, the error I am getting is telling me that I am trying to perform calculations on an incompatible data type. I tried to use a for-if nest to see and convert the non-float data to float:

```julia
begin
	algeria = df[df."Country/Region" .== "Algeria", 4:end]
	
	for i = 4:size(algeria, 2)
    	if eltype(algeria[!, i]) .!= Float64
        	algeria[!, i] = parse.(Float64, algeria[!, i])
    	end
	end
	
	Statistics.mean(eachcol(algeria))
end

```

But the error persists.

This is my full error:

```julia
MethodError: no method matching parse(::Type{Float64}, ::Array{Union{Missing, Int64},1})

Closest candidates are:

parse(::Type{T}, !Matched::AbstractString; kwargs...) where T<:Real at parse.jl:376

```

---

<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 19, 2021, 12:20am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/2 "2021-03-19T00:20:31Z")

</div>

I think that the code you are running and the code in this snippet are not the same.

The line

```julia
    	if eltype(algeria[!, i]) .!= Float64

```

should actually error, I think. And the fact that it doesn’t is odd.

On the other hand, the call `parse.(Float64, algeria[!, i])` should _not_ error, but is actually throwing the error that you just showed.

Here is an MWE that does what you want

```julia
julia> begin 
       df = DataFrame()
       N = 100
       df."Country/Region" = fill("Algeria", N)
       df.x_float = rand(N)
       df.x_string = [string.(rand(N-1)); missing]
       
       for i in 2:size(df, 2)
           v = df[!, i]
           if eltype(v) != Float64
               df[!, i] = passmissing(parse).(Float64, v)
           end
       end
       
       mean(eachcol(df[:, 2:end]))
       end

```

Note that you wan to use `passmissing(parse)` instead of just `parse` to deal with `missing` values properly.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 19, 2021, 12:29am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/3 "2021-03-19T00:29:18Z")

</div>

> [@pdeffebach](#):
>
> The line
> 
> ```julia
> if eltype(algeria[!, i]) .!= Float64
> 
> ```
> 
> should actually error, I think. And the fact that it doesn’t is odd.

No, dot operators are equivalent to scalar operators if all of the arguments are scalars. That is, it works for the same reason that `sqrt.(4)` is `2.0` (and `sqrt.(4) .== 2.0` is `true`).

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 12:34am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/4 "2021-03-19T00:34:41Z")

</div>

My Dataframe is Parquet in Arrow so idk if `df = DataFrame()` is soething I want.  
What is N? Why is it 100?  
Why are you taking a random float of N?

Do you mind explaining your code?

---

<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 19, 2021, 12:37am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/5 "2021-03-19T00:37:06Z")

</div>

> My Dataframe is Parquet in Arrow so idk if `df = DataFrame()` is soething I want.

Just to be clear, and I think this has been mentioned in other threads, once you read something into memory, _it is all the same_. A `DataFrame` is a `DataFrame`, and it doesn’t matter whether it came from Parquet, CSV, or was created in the code like I did above. Parquet in Arrow does not mean anything once you have a DataFrame.

My code creates a DataFrame with 100 rows, which is why `N` is 100.

`rand(N)` just creates a vector of length `N` of random numbers.

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:04am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/6 "2021-03-19T01:04:55Z")

</div>

Why are you creating a dataframe with 100 rows? I already have a full dataframe…

---

<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 19, 2021, 1:07am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/7 "2021-03-19T01:07:56Z")

</div>

I’m merely creating a small example do show you how the code works. That’s all. `100` was just an arbitrary number.

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:17am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/8 "2021-03-19T01:17:57Z")

</div>

passmissing is not defined

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 19, 2021, 1:27am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/9 "2021-03-19T01:27:13Z")

</div>

the error is telling you that you can’t `parse` Int64. `parse` is used to process text (`String`) to number (float or int). In this case you probably want:

```julia
algeria[!, i] = float.(algeria[!, i])

```

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:30am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/10 "2021-03-19T01:30:23Z")

</div>

`MethodError: no method matching AbstractFloat(::String)`

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 19, 2021, 1:33am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/11 "2021-03-19T01:33:31Z")

</div>

can you paste the result of:

```julia
describe(df)

```

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:36am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/12 "2021-03-19T01:36:46Z")

</div>

Describe isn’t defined. How can I import it?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 19, 2021, 1:37am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/13 "2021-03-19T01:37:11Z")

</div>

it’s from DataFrames.jl, are you not `using` DataFrames? if you only `import`ed, just do `DataFrames.describe`

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:38am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/14 "2021-03-19T01:38:53Z")

</div>

![Screenshot from 2021-03-18 21-38-15](https://global.discourse-cdn.com/julialang/original/3X/4/b/4bbd487cf8241fa32169bbf9eccca368318fbdf0.png)

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 19, 2021, 1:39am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/15 "2021-03-19T01:39:22Z")

</div>

I want to see the `eltype` column, in its entirety.

edit: if it’s too long, I guess `unique(DataFrames.describe(df).eltype)` will do.

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:43am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/16 "2021-03-19T01:43:48Z")

</div>

```julia
Union{Missing, Int64}
Union{Missing, String}
Union{Missing, Float64}

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 19, 2021, 1:47am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/17 "2021-03-19T01:47:45Z")

</div>

ok, so you must be including some columns you shouldn’t be including by doing

```julia
for i = 4:size(algeria, 2)

```

The issue is, it looks like you’re parsing columns that are not meant to be number. You can try this:

```julia
if eltype(algeria[!, i]) .!= Float64
    try
        algeria[!, i] = float.(algeria[!, i])
    catch
        println(i)
    end
end

```

then once you find that `i`th column is offending, `df[!, i]` and see what’s the content of this column and if it’s actually intended to be understood as number.

The only columns that contain `String` are: `Symbol("Province/State")` and `Symbol("Country/Region")`, which I don’t think can be understood as numbers to begin with.

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:48am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/18 "2021-03-19T01:48:44Z")

</div>

The if block goes inside the for loop or not?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 19, 2021, 1:50am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/19 "2021-03-19T01:50:09Z")

</div>

yes.

Also, I think it would be easier if you can share the data and provide a complete example script includes parsing etc. I guess you data is this? [https://github.com/CSSEGISandData/COVID-19/blob/master/csse\_covid\_19\_data/csse\_covid\_19\_time\_series/time\_series\_covid19\_confirmed\_global.csv](https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv)

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 19, 2021, 1:56am UTC](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507/20 "2021-03-19T01:56:24Z")

</div>

Yes, that is my data. And here is what I did:

```julia
begin
	algeria = df[df."Country/Region" .== "Algeria", 4:end]
	findall(eltype.(df[!, i] for i = 1:size(df, 2)) .!= Float64)
	
	# for i in 2:size(df, 2)
	# v = df[!, i]
	# if eltype(v) != Float64
	# df[!, i] = passmissing(parse).(Float64, v)
	# end
	# end
	
	for i = 4:size(algeria, 2)
		if eltype(algeria[!, i]) .!= Float64
    		try
        		algeria[!, i] = float.(algeria[!, i])
    		catch
        		println(i)
    		end
		end
	end
	
	Statistics.mean(eachcol(algeria))
end

```

Its throwing this error:

```julia
MethodError: no method matching +(::Float64, ::String)

Closest candidates are:

+(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:538

+(::Float64, !Matched::Float64) at float.jl:401

+(!Matched::ChainRulesCore.One, ::Any) at /home/onur/.julia/packages/ChainRulesCore/7d1hl/src/differential_arithmetic.jl:94

```

[Next page](https://discourse.julialang.org/t/how-to-parse-convert-integers-in-dataframe-to-float-numbers/57507.md?page=2)
