Classification of continious data
<p>I've got a Pandas df that I use for Machine Learning in Scikit for Python.
One of the columns is a target value which is continuous data (varying from -10 to +10).</p>

<p>From the target-column, I want to calculate a new column with 5 classes where the number of rows per class is the same, i.e. if I have 1000 rows I want to distribute into 5 classes with roughly 200 in each class.</p>

<p>So far, I have done this in Excel, separate from my Python code, but as the data has grown it's getting unpractical.</p>

<p>In Excel I have calculated the percentiles and then used some logic to build the classes.</p>

<p>How to do this in Python?</p>

<pre><code>#create data
import numpy as np
import pandas as pd
df = pd.DataFrame(20*np.random.rand(50, 1)-10, columns=['target'])   

#find quantiles
quantiles = df['target'].quantile([.2, .4, .6, .8])
#labeling of groups
df['group'] = 5
df['group'][df['target'] &lt; quantiles[.8]] = 4
df['group'][df['target'] &lt; quantiles[.6]] = 3       
df['group'][df['target'] &lt; quantiles[.4]] = 2 
df['group'][df['target'] &lt; quantiles[.2]] = 1 
</code></pre>

python scikit-learn percentile