Colanta Colanta - 2 months ago 30
Python Question

Frequency Table by range in pandas

I have two arrays in Python with random numbers:

vn = np.random.normal(20,5,500);
vu = np.random.uniform(17,25,500);


I'm trying to create a Frecuency table with pandas to have a count of the ocurrences by range, but i really have no idea on how to do it, an example input and output would look like:

input:

vn: [2,3,6,6,7,8,9,9,10,7]
vu: [1,1,2,3,6,7,7,7,8,9]


output:

Range count_vn count_vu
(0, 5] 2 4
(5, 10] 8 6


Any help would be appreciated

Answer Source

IIUC:

In [228]: df.apply(lambda x: pd.cut(x, bins=[0,5,10]).value_counts()).add_prefix('count_')
Out[228]:
         count_vn  count_vu
(5, 10]         8         6
(0, 5]          2         4