pandas.cut#

pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False, duplicates='raise', ordered=True)[源代码]#

将值分箱到离散区间。

当您需要分割和排序数据值到箱(bins)中时,请使用 cut。此函数也用于将连续变量转换为分类变量。例如,cut 可以将年龄转换为年龄段分组。支持等数量分箱或预设分箱数组。

Parameters:
x类数组

要进行分箱的输入数组。必须是一维的。

binsint, 标量序列,或 IntervalIndex

用于分箱的标准。

  • int : 定义 x 范围内的等宽箱的数量。x 的范围在两侧各扩展 0.1% 以包含 x 的最小值和最大值。

  • 标量序列 : 定义箱的边界,允许非均匀宽度。不扩展 x 的范围。

  • IntervalIndex : 定义要使用的确切箱。请注意,bins 的 IntervalIndex 必须不重叠。

rightbool, default True

指示 bins 是否包含最右边的边界。如果 right == True``(默认值),则 `bins` ``[1, 2, 3, 4] 表示 (1,2], (2,3], (3,4]。当 bins 是 IntervalIndex 时,忽略此参数。

labelsarray 或 False,默认 None

为返回的箱指定标签。必须与生成的箱长度相同。如果为 False,则仅返回箱的整数指示符。这会影响输出容器的类型(见下文)。当 bins 是 IntervalIndex 时,忽略此参数。如果为 True,则引发错误。当 ordered=False 时,必须提供标签。

retbinsbool,默认 False

是否返回箱。当 bins 是标量值时很有用。

precisionint,默认 3

存储和显示箱标签的精度。

include_lowestbool,默认 False

第一个区间是否应该是左闭的。

duplicates{默认 ‘raise’, ‘drop’}, optional

如果箱的边界不唯一,则引发 ValueError 或删除非唯一边界。

orderedbool, default True

标签是否是有序的。适用于返回的 Categorical 和 Series(带有 Categorical dtype)类型。如果为 True,则生成的分类将是有序的。如果为 False,则生成的分类将是无序的(必须提供标签)。

Returns:
outCategorical, Series, 或 ndarray

一个类数组对象,表示 x 中每个值的相应箱。类型取决于 labels 的值。

  • None(默认):对于 Series x 返回 Series,对于所有其他输入返回 Categorical。存储在其中的值是 Interval dtype。

  • 标量序列:对于 Series x 返回 Series,对于所有其他输入返回 Categorical。其中存储的值是序列中的类型。

  • False:返回一个整数 ndarray。

binsnumpy.ndarray 或 IntervalIndex。

计算或指定的箱。仅当 retbins=True 时返回。对于标量或序列 bins,这是一个包含计算出的箱的 ndarray。如果设置 duplicates=dropbins 将删除非唯一的箱。对于 IntervalIndex bins,这等于 bins

参见

qcut

基于排名或样本分位数将变量离散化为大小相等的桶。

Categorical

用于存储来自固定值集合的数据的数组类型。

Series

带有轴标签的一维数组(包括时间序列)。

IntervalIndex

实现有序、可切片集合的不可变索引。

Notes

任何 NA 值将在结果中为 NA。超出范围的值将在生成的 Series 或 Categorical 对象中为 NA。

参考 the user guide 获取更多示例。

Examples

将变量离散化为三个等大的箱。

>>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3)
... 
[(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ...
Categories (3, interval[float64, right]): [(0.994, 3.0] < (3.0, 5.0] ...
>>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3, retbins=True)
... 
([(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ...
Categories (3, interval[float64, right]): [(0.994, 3.0] < (3.0, 5.0] ...
array([0.994, 3.   , 5.   , 7.   ]))

发现相同的箱,但为它们分配特定的标签。请注意,返回的 Categorical 的类别是 labels 并且是有序的。

>>> pd.cut(np.array([1, 7, 5, 4, 6, 3]),
...        3, labels=["bad", "medium", "good"])
['bad', 'good', 'medium', 'medium', 'good', 'bad']
Categories (3, object): ['bad' < 'medium' < 'good']

ordered=False 在传递标签时将导致无序类别。此参数可用于允许非唯一标签:

>>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3,
...        labels=["B", "A", "B"], ordered=False)
['B', 'B', 'A', 'A', 'B', 'B']
Categories (2, object): ['A', 'B']

labels=False 意味着您只想获得箱。

>>> pd.cut([0, 1, 1, 2], bins=4, labels=False)
array([0, 1, 1, 3])

将 Series 作为输入传递会返回一个具有分类 dtype 的 Series:

>>> s = pd.Series(np.array([2, 4, 6, 8, 10]),
...               index=['a', 'b', 'c', 'd', 'e'])
>>> pd.cut(s, 3)
... 
a    (1.992, 4.667]
b    (1.992, 4.667]
c    (4.667, 7.333]
d     (7.333, 10.0]
e     (7.333, 10.0]
dtype: category
Categories (3, interval[float64, right]): [(1.992, 4.667] < (4.667, ...

将 Series 作为输入传递会返回一个具有映射值的 Series。它用于根据箱将数值映射到区间。

>>> s = pd.Series(np.array([2, 4, 6, 8, 10]),
...               index=['a', 'b', 'c', 'd', 'e'])
>>> pd.cut(s, [0, 2, 4, 6, 8, 10], labels=False, retbins=True, right=False)
... 
(a    1.0
 b    2.0
 c    3.0
 d    4.0
 e    NaN
 dtype: float64,
 array([ 0,  2,  4,  6,  8, 10]))

当箱不唯一时,使用 drop 可选参数

>>> pd.cut(s, [0, 2, 4, 6, 10, 10], labels=False, retbins=True,
...        right=False, duplicates='drop')
... 
(a    1.0
 b    2.0
 c    3.0
 d    3.0
 e    NaN
 dtype: float64,
 array([ 0,  2,  4,  6, 10]))

bins 传递 IntervalIndex 会精确得到那些类别。请注意,未被 IntervalIndex 覆盖的值设置为 NaN。0 在第一个箱(右闭)的左侧,1.5 落在两个箱之间。

>>> bins = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)])
>>> pd.cut([0, 0.5, 1.5, 2.5, 4.5], bins)
[NaN, (0.0, 1.0], NaN, (2.0, 3.0], (4.0, 5.0]]
Categories (3, interval[int64, right]): [(0, 1] < (2, 3] < (4, 5]]