# I want to bin numerical values into manually set percentile values, how can I do this?

**URL:** <https://discourse.julialang.org/t/i-want-to-bin-numerical-values-into-manually-set-percentile-values-how-can-i-do-this/35651>\
**Category:** General Usage\
**Created:** [March 6, 2020, 5:11pm UTC](https://discourse.julialang.org/t/i-want-to-bin-numerical-values-into-manually-set-percentile-values-how-can-i-do-this/35651 "2020-03-06T17:11:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [March 6, 2020, 5:11pm UTC](https://discourse.julialang.org/t/i-want-to-bin-numerical-values-into-manually-set-percentile-values-how-can-i-do-this/35651/1 "2020-03-06T17:11:16Z")

</div>

Hi how’s it going?

I have a numerical column that I’d like to bin into these percentile bin edges:

percentiles = [.05,.35,.8,.95]

In Python it’s as easy as pd.qcut. I was wondering what the simplest way would be for me to do this - I’ve found Julia solutions where it automatically creates the bins for you, but I would like something where I can manually set the bin edges.

Thank you!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 6, 2020, 5:49pm UTC](https://discourse.julialang.org/t/i-want-to-bin-numerical-values-into-manually-set-percentile-values-how-can-i-do-this/35651/2 "2020-03-06T17:49:46Z")

</div>

Look at [`cut`](https://juliadata.github.io/CategoricalArrays.jl/stable/using.html#Working-with-categorical-arrays-1) from `CategoricalArrays`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 7, 2020, 1:07pm UTC](https://discourse.julialang.org/t/i-want-to-bin-numerical-values-into-manually-set-percentile-values-how-can-i-do-this/35651/4 "2020-03-07T13:07:16Z")

</div>

Eg something like

```julia
function bin_by_percentiles(x, p)
    @assert issorted(p)
    q = quantile(x, p; sorted = true)
    searchsortedfirst.(Ref(q), x)
end

bin_by_percentiles(randn(50), [.05,.35,.8,.95])

```
