1、pd.cut()
用于将数据值按照值本身进行分段并排序到 bins 中。
参数包含:x, bins, right, include_lowest, labels, retbins, precision
x :被划分的数组
bins :被划分的区间/区间数
- - ① 当 bins 为整数时,表示数组 x 被划分为多少个等间距的区间;
- - ② 当 bins 为序列时,表示数组 x 将被划分在该指定序列中,若不在则输出 NaN;
# x = [1,2,3,5,3,4,1], bins = 3 [In ] pd.cut(np.array([1,2,3,5,3,4,1]),3) [Out] [(0.996, 2.333], (0.996, 2.333], (2.333, 3.667], (3.667, 5.0], (2.333, 3.667], (3.667, 5.0], (0.996, 2.333]] Categories (3, interval[float64]): [(0.996, 2.333] < (2.333, 3.667] < (3.667, 5.0]] # x = [1,2,3,5,3,4,1], bins = [1,2,3] [In ] pd.cut(np.array([1,2,3,5,3,4,1]),[1,2,3]) [Out] [NaN, (1.0, 2.0], (2.0, 3.0], NaN, (2.0, 3.0], NaN, NaN] Categories (2, interval[int64]): [(1, 2] < (2, 3]]
right :是否包含右端点,默认为 True;
include_lowest :是否包含左端点,默认为 False;
# x = [1,2,3,5,3,4,1], bins = [1,2,3], 默认不包含左端点 1,默认包含右端点 3 [In ] pd.cut(np.array([1,2,3,5,3,4,1]),[1,2,3]) [Out] [NaN, (1.0, 2.0], (2.0, 3.0], NaN, (2.0, 3.0], NaN, NaN] Categories (2, interval[int64]): [(1, 2] < (2, 3]] # x = [1,2,3,5,3,4,1], bins = [1,2,3], 设置包含左端点 1,设置包含右端点 3 [In ] pd.cut(np.array([1,2,3,5,3,4,1]),[1,2,3],include_lowest=True,right=False) [Out] [[1.0, 2.0), [2.0, 3.0), NaN, NaN, NaN, NaN, [1.0, 2.0)] Categories (2, interval[int64]): [[1, 2) < [2, 3)]
labels :是否用标记来替代返回的 bins,默认为 False,如需标记,标记数需与 bins 数一致,并为 labels 赋值一组列表;
# x = [1,2,3,5,3,4,1], bins = 3, 设置用指定标签 ['A','B','C'] 返回序列 [In ] pd.cut(np.array([1,2,3,5,3,4,1]),3,labels=['A','B','C']) [Out] [A, A, B, C, B, C, A] Categories (3, object): [A < B < C]
retbins : 是否返回间距 bins,默认为 False,仅返回 x 中每个值对应的 bin 的列表,若 retbins = True,则返回 bin 的列表及对应的 bins。
# x = [1,2,3,5,3,4,1], bins = 3, 设置一并返回对应 bins 序列 [In ] pd.cut(np.array([1,2,3,5,3,4,1]),3,retbins=True) [Out] ([(0.996, 2.333], (0.996, 2.333], (2.333, 3.667], (3.667, 5.0], (2.333, 3.667], (3.667, 5.0], (0.996, 2.333]] Categories (3, interval[float64]): [(0.996, 2.333] < (2.333, 3.667] < (3.667, 5.0]], array([0.996 , 2.33333333, 3.66666667, 5. ]))
precision : 精度,区间边界值保留的小数点位数
# x = [1,2,3,5,3,4,1], bins = 3, 精度为2 [In ] pd.cut(np.array([1,2,3,5,3,4,1]),3,precision=2) [Out] [(1.0, 2.33], (1.0, 2.33], (2.33, 3.67], (3.67, 5.0], (2.33, 3.67], (3.67, 5.0], (1.0, 2.33]] Categories (3, interval[float64]): [(1.0, 2.33] < (2.33, 3.67] < (3.67, 5.0]]
2、pd.qcut()
基于分位数的离散化功能。 根据等级或基于样本分位数(或者说基于样本值落在区间的频率),将变量分离为相等大小的桶。
参数包含:x, q, labels, retbins, precision, duplicates
- x、labels、retbins、precision 与 pd.cut() 中参数用法一致;
- q : 将序列 x 划分为 q 个区间,使落在每个区间的值的数量一致;
- duplicates :重复值处理,默认为 duplicates = ‘raise’,表示不忽略重复值。如需忽略 x 中的重复值,可指定 duplicates = ‘drop’。
3、pd.cut() v.s. pd.qcut()
- pd.cut() 将指定序列 x,按指定数量等间距的划分(根据值本身而不是这些值的频率选择均匀分布的bins),或按照指定间距划分
- pd.qcut() 将指定序列 x,划分为 q 个区间,使落在每个区间的记录数一致
[In] ll = [1,2,3,5,3,4,1,2] print('- - - pd.cut()示例1 - - -') print(pd.cut(ll, 4, precision=2).value_counts()) print('- - - pd.cut()示例2 - - -') print(pd.cut(ll, [1,2,4], precision=2).value_counts()) print('- - - pd.qcut()示例 - - -') print(pd.qcut(ll, 4, precision=2).value_counts()) [Out] - - - pd.cut()示例1 - - - (1.0, 2.0] 4 (2.0, 3.0] 2 (3.0, 4.0] 1 (4.0, 5.0] 1 dtype: int64 - - - pd.cut()示例2 - - - (1, 2] 2 (2, 4] 3 dtype: int64 - - - pd.qcut()示例 - - - (0.99, 1.75] 2 (1.75, 2.5] 2 (2.5, 3.25] 2 (3.25, 5.0] 2 dtype: int64
到此这篇关于python中pd.cut()与pd.qcut()的对比及示例的文章就介绍到这了,更多相关python pd.cut()与pd.qcut()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!