# I want to extract and convert to integer a column in a DataFrame

**URL:** <https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566>\
**Category:** Data\
**Tags:** data\
**Created:** [August 26, 2017, 1:50am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566 "2017-08-26T01:50:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Victor](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jean-Victor](https://discourse.julialang.org/u/Jean-Victor)\
**Post date:** [August 26, 2017, 1:50am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/1 "2017-08-26T01:50:47Z")

</div>

With Julia 0.5, I could do this:  
Calories=int(Nutrition[2:11,2]), where Nutrition is a DataFrame.

With Julia 0.6, whatever I do of the kind spawns an ominous red box which floats over my program like a giant cloud in an otherwise sunny sky.

---

<div class="post-metadata">

**Author:** ![amellnik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amellnik/32/137_2.png) [@amellnik](https://discourse.julialang.org/u/amellnik)\
**Post date:** [August 26, 2017, 1:59am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/2 "2017-08-26T01:59:15Z")

</div>

Use the broadcasted form of `convert`:

```julia
df = DataFrame(a = [1.0, 2.0, 3.0], b=[2.0, 3.0, 4.0])
convert.(Int64, df[2:3,:a])

```

results in

```julia
2-element DataArrays.DataArray{Int64,1}:
 2
 3

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 26, 2017, 2:01am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/3 "2017-08-26T02:01:14Z")

</div>

I am not sure I got your question, but the way you convert pieces of dataframes to arrays is by using:

```julia
A = convert(Array, df)

```

---

<div class="post-metadata">

**Author:** ![Jean-Victor](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jean-Victor](https://discourse.julialang.org/u/Jean-Victor)\
**Post date:** [August 26, 2017, 2:18am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/4 "2017-08-26T02:18:10Z")

</div>

I tried this, but there must be something wrong with the file, since I got the following error message →

LoadError: MethodError: Cannot `convert` an object of type String to an object of type Int64  
This may have arisen from a call to the constructor Int64(…),  
since type constructors fall back to convert methods.

---

<div class="post-metadata">

**Author:** ![amellnik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amellnik/32/137_2.png) [@amellnik](https://discourse.julialang.org/u/amellnik)\
**Post date:** [August 26, 2017, 2:52am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/5 "2017-08-26T02:52:52Z")

</div>

Sounds like you need `parse` in place of `convert`.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 26, 2017, 4:15am UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/6 "2017-08-26T04:15:17Z")

</div>

@Jean-Victor, have you tried the simple convert(Array, df[1:2,:a]) I mentioned? It will convert the column preserving the eltype. In most cases, you don’t need to do this though, DataArrays returned by indexing DataFrames work seamlessly with many operations defined on AbstractArray

---

<div class="post-metadata">

**Author:** ![Jean-Victor](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jean-Victor](https://discourse.julialang.org/u/Jean-Victor)\
**Post date:** [August 26, 2017, 12:59pm UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/7 "2017-08-26T12:59:13Z")

</div>

I tried this, but unfortunately it is interpreted as a string vector, yielding this error message when I use it as if it was an integer vector →

LoadError: MethodError: no method matching \*(::JuMP.Variable, ::String)

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [August 26, 2017, 5:49pm UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/8 "2017-08-26T17:49:14Z")

</div>

I think you need to tell us more about the structure of the `Nutrition` data frame on Julia 0.5 vs. 0.6, and how you create it.

---

<div class="post-metadata">

**Author:** ![Jean-Victor](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jean-Victor](https://discourse.julialang.org/u/Jean-Victor)\
**Post date:** [August 26, 2017, 7:00pm UTC](https://discourse.julialang.org/t/i-want-to-extract-and-convert-to-integer-a-column-in-a-dataframe/5566/9 "2017-08-26T19:00:02Z")

</div>

There was something wrong with the file: the title had 2 lines. With Julia 0.5, using the same file, skipping the second line of the title was possible before conversion was attempted, whereas it doesn’t seem to be possible in Julia 0.6 because the whole set of lines of the dataframe seems to be checked for consistency before converting even part of it containing lines that can be converted. Removing one of the lines of the title and selecting lines 1 to 10 instead of lines 2 to 11 solves the problem.

Using Julia 0.6, there was also a problem with empty lines at the end of the file because the conversion did not handle NAs (by default, I suppose), which I also removed before attempting the conversion.

With the cleaned up file, both of your solutions worked flawlessly. Thank you both!
