# How to change the data type of a specific column of a dataframe?

**URL:** https://discourse.julialang.org/t/how-to-change-the-data-type-of-a-specific-column-of-a-dataframe/34598
**Category:** General Usage
**Tags:** first-steps
**Created:** [February 13, 2020, 6:58pm UTC](https://discourse.julialang.org/t/how-to-change-the-data-type-of-a-specific-column-of-a-dataframe/34598 "2020-02-13T18:58:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 13, 2020, 6:58pm UTC](https://discourse.julialang.org/t/how-to-change-the-data-type-of-a-specific-column-of-a-dataframe/34598/1 "2020-02-13T18:58:59Z")

</div>

Hi

I have a dataframe of 5 columns. All of them are of type “string”.  
I want to convert column 2 (“count”) from String to Int 16.

So far I have tried:

```
df_splunkie[:count]=Int16(df_splunkie[:count])

```

This is the error:

```julia
┌ Warning: `getindex(df::DataFrame, col_ind::ColumnIndex)` is deprecated, use `df[!, col_ind]` instead.
│ caller = top-level scope at In[94]:4
└ @ Core In[94]:4

MethodError: no method matching Int16(::Array{String,1})
Closest candidates are:
  Int16(!Matched::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:708
  Int16(!Matched::Float32) at float.jl:685
  Int16(!Matched::Float64) at float.jl:685
  ...

Stacktrace:
 [1] top-level scope at In[94]:4

```

Also this:

```
df_splunkie[!, 2]= Int16(df_splunkie[!, 2])

```

And this is the error:

```julia
MethodError: no method matching Int16(::Array{String,1})
Closest candidates are:
  Int16(!Matched::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:708
  Int16(!Matched::Float32) at float.jl:685
  Int16(!Matched::Float64) at float.jl:685
  ...

Stacktrace:
 [1] top-level scope at In[95]:4

```

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 13, 2020, 7:19pm UTC](https://discourse.julialang.org/t/how-to-change-the-data-type-of-a-specific-column-of-a-dataframe/34598/2 "2020-02-13T19:19:48Z")

</div>

You are correct that you need to use `df[!,:name]` to access a particular column, not just `df[:name]`. You are missing that you cannot “directly” convert from string to integer. You need to use `parse`.

```julia
df[!,2] .= parse.(Int,df[!,2])

```

I think that should do it?

---

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 13, 2020, 7:32pm UTC](https://discourse.julialang.org/t/how-to-change-the-data-type-of-a-specific-column-of-a-dataframe/34598/3 "2020-02-13T19:32:31Z")

</div>

What does “parse” do? What is the difference from “convert”?

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 13, 2020, 7:35pm UTC](https://discourse.julialang.org/t/how-to-change-the-data-type-of-a-specific-column-of-a-dataframe/34598/4 "2020-02-13T19:35:03Z")

</div>

You should type `? parse` to see the docs. The first line says “Parse a string as a number.”

`convert` is for number-to-number type conversions (Float → Int, etc.). It can also be used to change array types.
