# Discretize/Binning of Continuous Variable in Dataframe

**URL:** <https://discourse.julialang.org/t/discretize-binning-of-continuous-variable-in-dataframe/66052>\
**Category:** New to Julia\
**Created:** [August 9, 2021, 2:35am UTC](https://discourse.julialang.org/t/discretize-binning-of-continuous-variable-in-dataframe/66052 "2021-08-09T02:35:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nelson\_Chow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nelson_chow/32/27288_2.png) [@Nelson\_Chow](https://discourse.julialang.org/u/Nelson_Chow)\
**Post date:** [August 9, 2021, 2:35am UTC](https://discourse.julialang.org/t/discretize-binning-of-continuous-variable-in-dataframe/66052/1 "2021-08-09T02:35:03Z")

</div>

Hello There,

I am just wondering what is the best way to add a new Categorical/String column to existing dataframe based on a continuous value column?

‘using DataFrames  
df = DataFrame(Types = [“SUV”, “SUV”,“SUV”, “SUV”, “sedan”,“sedan”], models=[“Q3”,“Q5”, “Kluger”, “Land Cruiser”, “Corolla”, “F40”], acceleration = [11,8, 8, 19, 5.5,3.3,])’

6×3 DataFrame  
Row │ Types models acceleration  
│ String String Float64  
─────┼────────────────────────────────────  
1 │ SUV Q3 11.0  
2 │ SUV Q5 8.0  
3 │ SUV Kluger 8.0  
4 │ SUV Land Cruiser 19.0  
5 │ sedan Corolla 5.5  
6 │ sedan F40 3.3

Want:  
Acceleration less than 6: “Fast”  
Acceleration greater than 10 “Slow”  
else “Normal”

sorry, i know this is most basic, and in Python, i would have used .apply of a custom function. I did a 20 min search on Goggle but couldn’t find anything.

Thanks!

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 9, 2021, 2:41am UTC](https://discourse.julialang.org/t/discretize-binning-of-continuous-variable-in-dataframe/66052/2 "2021-08-09T02:41:00Z")

</div>

`CategoricalArrays.cut` [Using CategoricalArrays · CategoricalArrays](https://categoricalarrays.juliadata.org/dev/using/)

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 9, 2021, 6:05am UTC](https://discourse.julialang.org/t/discretize-binning-of-continuous-variable-in-dataframe/66052/3 "2021-08-09T06:05:33Z")

</div>

Hi @Nelson_Chow

I would typically use map

```julia
julia> c = map(x -> x<6 ? "Fast" : (x>10 ? "Slow" : "Normal"), 
               df[:,"acceleration"])
julia> insertcols!(df,ncol(df)+1,:speed=>c)
6×4 DataFrame
 Row │ Types models acceleration speed  
     │ String String Float64 String 
─────┼────────────────────────────────────────────
   1 │ SUV Q3 11.0 Slow
   2 │ SUV Q5 8.0 Normal
   3 │ SUV Kluger 8.0 Normal
   4 │ SUV Land Cruiser 19.0 Slow
   5 │ sedan Corolla 5.5 Fast
   6 │ sedan F40 3.3 Fast

```

Also, pre-formatted text is very helpful when sharing code 🙂
