pandas.cut#
- pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False, duplicates='raise', ordered=True)[源代码]#
将值分箱到离散区间。
当您需要分割和排序数据值到分箱中时,请使用 cut。此函数还可用于将连续变量转换为分类变量。例如,cut 可以将年龄转换为年龄组。支持分箱为相同数量的分箱,或预先指定的箱数组。
- 参数:
- x一维 ndarray 或 Series
要分箱的输入数组。必须是一维的。
- binsint, 标量序列, 或 IntervalIndex
用于分箱的标准。
int : 定义 x 范围内的等宽分箱数。 x 的范围在两端各扩展 0.1%,以包含 x 的最小值和最大值。
标量序列 : 定义分箱边界,允许非均匀宽度。不对 x 的范围进行扩展。
IntervalIndex : 定义要使用的确切分箱。请注意,bins 的 IntervalIndex 必须是非重叠的。
- rightbool, 默认 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 时,必须提供 labels。
- retbinsbool, 默认 False
是否返回分箱。当 bins 以标量形式提供时很有用。
- precisionint, 默认 3
存储和显示分箱标签的精度。
- include_lowestbool, 默认 False
第一个区间是否应包含左边界。
- duplicates{‘raise’, ‘drop’}, 默认 ‘raise’
如果分箱边界不唯一,则引发 ValueError 或删除非唯一值。
- orderedbool, 默认 True
标签是否已排序。适用于返回的 Categorical 和 Series(具有 Categorical dtype)类型。如果为 True,则生成的分类将是有序的。如果为 False,则生成的分类将是无序的(必须提供标签)。
- 返回:
- outCategorical, Series, 或 ndarray
表示 x 的每个值对应的分箱的类数组对象。类型取决于 labels 的值。
None(默认值):对于 Series x,返回一个 Series;对于所有其他输入,返回一个 Categorical。存储在其中的值是 Interval dtype。
标量序列:对于 Series x,返回一个 Series;对于所有其他输入,返回一个 Categorical。存储在其中的值是序列中的任何类型。
False:返回一个一维 ndarray 或 Series 整数。
- binsnumpy.ndarray 或 IntervalIndex.
计算出的或指定的箱。仅当 retbins=True 时返回。对于标量或序列 bins,这是一个包含计算出的箱的 ndarray。如果设置为
duplicates=drop,bins 将删除非唯一的箱。对于 IntervalIndex bins,此值等于 bins。
另请参阅
qcut根据排名或样本分位数将变量离散化为大小相等的桶。
Categorical用于存储来自固定值集合的数据的数组类型。
Series带有轴标签的一维数组(包括时间序列)。
IntervalIndex不可变的 Index,实现了有序、可切片的集合。
numpy.histogram_bin_edges仅计算直方图函数所用分箱边缘的函数。
注意
任何 NA 值在结果中都将是 NA。超出范围的值在结果 Series 或 Categorical 对象中将是 NA。
numpy.histogram_bin_edges可以与 cut 一起使用,根据某些预定义的方法计算箱。有关更多示例,请参阅 用户指南。
示例
离散化为三个大小相等的箱。
>>> 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, str): ['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, str): ['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]))
将 IntervalIndex 用作 bins 会导致完全相同的类别。请注意,未被 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]]
将 np.histogram_bin_edges 与 cut 一起使用
>>> pd.cut( ... np.array([1, 7, 5, 4]), ... bins=np.histogram_bin_edges(np.array([1, 7, 5, 4]), bins="auto"), ... ) ... [NaN, (5.0, 7.0], (3.0, 5.0], (3.0, 5.0]] Categories (3, interval[float64, right]): [(1.0, 3.0] < (3.0, 5.0] < (5.0, 7.0]]