Colanta Colanta - 6 months ago 75
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:


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


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

Any help would be appreciated

Answer Source


In [228]: df.apply(lambda x: pd.cut(x, bins=[0,5,10]).value_counts()).add_prefix('count_')
         count_vn  count_vu
(5, 10]         8         6
(0, 5]          2         4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download