Qcut in Pandas

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
l = np.arange(1, 20)
l
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19])
pd.qcut(l, q=[0, 0.2, 0.4, 1.0], labels=["small", "medium", "large"])
['small', 'small', 'small', 'small', 'medium', ..., 'large', 'large', 'large', 'large', 'large']
Length: 19
Categories (3, object): ['small' < 'medium' < 'large']
pd.qcut(l, q=[0, 0.2, 0.4, 1.0], labels=["small", "medium", "large"]).value_counts()
small      4
medium     4
large     11
dtype: int64
df = pd.DataFrame(pd.qcut(l, q=[0, 0.2, 0.4, 1.0], labels=["small", "medium", "large"]))
df
0
0 small
1 small
2 small
3 small
4 medium
5 medium
6 medium
7 medium
8 large
9 large
10 large
11 large
12 large
13 large
14 large
15 large
16 large
17 large
18 large
df.value_counts().plot(kind='barh')
plt.show()

png