# Replacing one column with three (DataFrames.jl)

**URL:** https://discourse.julialang.org/t/replacing-one-column-with-three-dataframes-jl/65565
**Category:** Data
**Tags:** dataframes
**Created:** [July 30, 2021, 7:00pm UTC](https://discourse.julialang.org/t/replacing-one-column-with-three-dataframes-jl/65565 "2021-07-30T19:00:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alex-ZA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-za/32/27612_2.png) [@Alex-ZA](https://discourse.julialang.org/u/Alex-ZA)
#### Post date: [July 30, 2021, 7:00pm UTC](https://discourse.julialang.org/t/replacing-one-column-with-three-dataframes-jl/65565/1 "2021-07-30T19:00:47Z")

</div>

Hi everyone! My first post here. I have to say, loving the Julia language. It is quickly becoming my favourite. Also, fantastic work with Juno. It is awesome.

I am trying to implement an “One Hot” transform on some data and would like to know if there is a way (in DataFrames.jl) to replace one column with three? Specifically by using select!() or transform!()?

The data:

![image](https://global.discourse-cdn.com/julialang/original/3X/1/1/11e09d1d622c18f3dcc13aa5da89d1ede0d3513f.png)

Some code:

```julia
# Create dataframe.
currDir = pwd()
filePath = string(currDir, "\\UdemyCourseML\\(1)DataPreprocessing\\data.csv");
df = DataFrame(CSV.File.(filePath))

# Define variables.
featX = df[:, 1:end-1]
featY = df.Purchased;

# Replace missing values with averages.
featX.Salary = coalesce.(featX.Salary, mean(skipmissing(featX.Salary)))
featX.Age = coalesce.(featX.Age, mean(skipmissing(featX.Age)))

# Encode Country data with one hot encoding.
onehotCountries = transpose(Flux.onehotbatch(featX.Country, unique(featX.Country)))

#select!() ?
#transform!() ?

```

Thanks!

---

<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: [July 30, 2021, 7:30pm UTC](https://discourse.julialang.org/t/replacing-one-column-with-three-dataframes-jl/65565/2 "2021-07-30T19:30:05Z")

</div>

You want `transform` with `AsTable`. Here is an MWE that doesn’t use `Flux.jl`, but should be basically the same

```julia
julia> function onehot(x)
       u = unique(x)
       df = DataFrame()
       for ui in u
           df[!, ui] = x .== ui
       end
       df
       end;

julia> df = DataFrame(s = rand(["up", "down", "left"], 10))
10×1 DataFrame
 Row │ s
     │ String
─────┼────────
   1 │ left
   2 │ left
   3 │ down
   4 │ left
   5 │ down
   6 │ up
   7 │ down
   8 │ up
   9 │ up
  10 │ left

julia> transform!(df, :s => onehot => AsTable)
10×4 DataFrame
 Row │ s left down up
     │ String Bool Bool Bool
─────┼─────────────────────────────
   1 │ left true false false
   2 │ left true false false
   3 │ down false true false
   4 │ left true false false
   5 │ down false true false
   6 │ up false false true
   7 │ down false true false
   8 │ up false false true
   9 │ up false false true
  10 │ left true false false

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [July 30, 2021, 10:29pm UTC](https://discourse.julialang.org/t/replacing-one-column-with-three-dataframes-jl/65565/3 "2021-07-30T22:29:14Z")

</div>

> [@Alex-ZA](#):
>
> “One Hot” transform

I have listed some ways of doing exactly this [All the ways to do one-hot encoding](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807)

> [@Alex-ZA](#):
>
> Juno

FYI. Juno is more or less not being developed for new features. VSCode is where the IDE dev works are concentrated.

---

<div class="post-metadata">

### Author: ![Alex-ZA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-za/32/27612_2.png) [@Alex-ZA](https://discourse.julialang.org/u/Alex-ZA)
#### Post date: [August 3, 2021, 11:19am UTC](https://discourse.julialang.org/t/replacing-one-column-with-three-dataframes-jl/65565/4 "2021-08-03T11:19:07Z")

</div>

> [@xiaodai](#):
>
> I have listed some ways of doing exactly this [All the ways to do one-hot encoding](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807)

I ended up using DataConvenience.jl, thank you!

```julia
# Encode country data with onehot encoding.
onehot!(featX, :Country, outnames=Symbol.(unique(featX.Country)))
select!(featX, (2:6))

```
