生成20个[0,100]的随机整数
编辑日期: 2024-11-28 文章阅读: 次
@author jackzhenguo
@desc cut 数据分箱
@tag
@version
@date 2020/11/28
第174个小例子:cut 数据分箱
将百分制分数转为A,B,C,D四个等级,bins 被分为 [0,60,75,90,100],labels 等于['D', 'C', 'B', 'A']:
In [30]: a = np.random.randint(1,100,20)
In [31]: a
Out[31]:
array([48, 22, 46, 84, 13, 52, 36, 35, 27, 99, 31, 37, 15, 31, 5, 46, 98,99, 60, 43])
In [33]: pd.cut(a, [0,60,75,90,100], labels = ['D', 'C', 'B', 'A'])
Out[33]:
[D, D, D, B, D, ..., D, A, A, D, D]
Length: 20
Categories (4, object): [D < C < B < A]
分箱后,48分对应D,22分对应D,46对应D,84分对应B,...