频率表(按 pandas 范围) [英] Frequency Table by range in pandas

查看:92
本文介绍了频率表(按 pandas 范围)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有两个随机数的数组:

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

任何帮助将不胜感激

推荐答案

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

或@ayhan提供的更好的解决方案:

or a nicer solution provided by @ayhan:

In [26]: df.apply(pd.Series.value_counts, bins=[0,5,10])
Out[26]:
               vn  vu
(5.0, 10.0]     8   6
(-0.001, 5.0]   2   4

它以某种方式产生了奇怪的"垃圾箱...

somehow it produced "strange" bins...

这篇关于频率表(按 pandas 范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆