Categorize each feature from a dataset by percentiles via python
<p>I'm trying to figure a way to categorize each column in my dataset based on it's percentiles. For example, consider the column:</p>

<pre><code> ticket
 24160
 113781
 113781
 113781
 113781
 19952
 13502
 112050
 11769
</code></pre>

<p>The 20th percentile of the column above is 1350. Basically I want to convert that column into a categorical variable where all values from the 0-20th percentile = 1, all values from the 20-40th percentile = 2, all values from the 40-60th percentile = 3 and so on. Thus the ticket feature will be a categorical variable with either 1,2,3,4 or 5. I want to apply this conversion to every column in my dataset besides the last column. So far I've coded:</p>

<pre><code>   import numpy as np
   import pandas as pd

   dataset = pd.read_csv('somedataset.csv')
   def func(x):
      if min(x)&lt;=x&lt; np.percentile(x, 20):
       return 1
      elif  np.percentile(x, 20)&lt;=x&lt; np.percentile(x, 40):
        return 2
     elif  np.percentile(x, 40)&lt;=x&lt; np.percentile(x, 60):
        return 3
     elif  np.percentile(x, 60)&lt;=x&lt; np.percentile(x, 80):
        return 4
     elif  x = max(x):
        return 5
    dataset[:]= dataset[:].apply(func)
</code></pre>

<p>I don't know how to apply this function to each column besides the last column within my dataset. I would greatly appreciate any feedback!</p>

<pre><code>np.floor(df[df.columns[:-1]].rank() / len(df) / .2).astype(int) + 1
</code></pre>

<p>The above code returns what you want, with the same column names as original data.</p>

<ol>
<li><code>df[df.columns[:-1]]</code> subsets all but the last column as you requested</li>
<li><code>.rank()</code> gives the integer rank of the item from smallest to largest</li>
<li><code>/ len(df) / .2</code> gives you the percentile bucket </li>
<li><code>np.floor(...).astype(int) + 1</code> gives you the bucket as an integer starting at 1</li>
</ol>

python