Pandas 的 qcut(~)
方法将数值分类为分位数箱(间隔),以便每个箱中的项目数量相等。
参数
1. x
| array-like
一个一维输入数组,其数值将被分段到容器中。
2. q
| int
或 sequence<number>
或 IntervalIndex
分位数的数量。如果 q=4
,则将计算四分位数。您还可以传入四分位数数组(例如 [0, 0.1, 0.5, 1]
]。
3. labels
| array
或 False
| optional
所需的箱子标签。默认情况下,labels=None
。
4. retbins
| boolean
| optional
是否返回箱子。默认情况下,retbins=False
。
5. precision
| int
| optional
容器标签中包含的小数位数。默认情况下,precision=3
。
6. duplicates
| string
| optional
如何处理重复的 bin 边:
值 |
说明 |
---|---|
|
如果设置了任何重复的 bin 边,则会抛出错误。 |
|
删除重复的箱子边并只保留一个。 |
默认情况下,duplicates="raise"
。
返回值
如果 retbins=False
,则返回类型取决于 labels
参数的值:
-
如果未指定
labels
,则返回对每个值的 bin 进行编码的Series
或Categorical
。 -
如果提供数组,则返回
Series
或Categorical
。 -
如果提供布尔值
False
,则返回 NumPy 整数数组。
如果 retbins=True
,则除了上述内容之外,容器还将作为 NumPy 数组返回。如果 x
是 IntervalIndex
,则返回 x
。
例子
考虑以下有关学生及其成绩的DataFrame:
raw_grades = [3,6,8,7,3,5]
students = ["alex", "bob", "cathy", "doge", "eric", "fred"]
df = pd.DataFrame({"name":students,"raw_grade":raw_grades})
df
name raw_grade
0 alex 3
1 bob 6
2 cathy 8
3 doge 7
4 eric 3
5 fred 5
基本用法
将原始等级分为四个箱(段):
df["grade"] = pd.qcut(df["raw_grade"], q=4)
df
name raw_grade grade
0 alex 3 (2.999, 3.5]
1 bob 6 (5.5, 6.75]
2 cathy 8 (6.75, 8.0]
3 doge 7 (6.75, 8.0]
4 eric 3 (2.999, 3.5]
5 fred 5 (3.5, 5.5]
这里的四个四分位数如下:
1st: (2.999, 3.5]
2nd: (3.5, 5.5]
3rd: (5.5, 6.75]
4th: (6.75, 8.0]
请注意, (2.995, 3.5]
仅表示 2.999 < raw_grade <= 3.5
。
指定四分位数
要指定自定义四分位数,我们可以传入四分位数 array
而不是 int
:
df["grade"] = pd.qcut(df["raw_grade"], q=[0, .4, .8, 1])
df
name raw_grade grade
0 alex 3 (2.999, 5.0]
1 bob 6 (5.0, 7.0]
2 cathy 8 (7.0, 8.0]
3 doge 7 (5.0, 7.0]
4 eric 3 (2.999, 5.0]
5 fred 5 (2.999, 5.0]
指定标签
我们可以通过设置labels
参数来为我们的箱子添加标签:
df["grade"] = pd.qcut(df["raw_grade"], q=4, labels=["D","C","B","A"])
df
name raw_grade grade
0 alex 3 D
1 bob 6 B
2 cathy 8 A
3 doge 7 A
4 eric 3 D
5 fred 5 C
这是qcut(~)
方法的一个非常实用的函数。这里,labels
数组的长度必须等于指定的四分位数。
指定 retbins
要获得计算的 bin 边,请设置 retbins=True
:
x = [3,6,8,7,4,5]
res = pd.cut(x, bins=2, retbins=True)
print("Categories: ", res[0])
print("Bin egdes: ", res[1])
Categories: [(2.999, 4.5], (4.5, 6.0], (6.75, 8.0], (6.75, 8.0], (2.999, 4.5], (4.5, 6.0]]
Categories (4, interval[float64]): [(2.999, 4.5] < (4.5, 6.0] < (6.0, 6.75] < (6.75, 8.0]]
Bin egdes: [ 3. 4.5 6. 6.75 8. ]
指定精度
为了控制显示多少位小数,请设置precision
参数:
x = [3,6,8,7,4,5]
bins = pd.qcut(x, q=4, precision=2)
print(bins)
[(2.99, 4.25], (5.5, 6.75], (6.75, 8.0], (6.75, 8.0], (2.99, 4.25], (4.25, 5.5]]
Categories (4, interval[float64]): [(2.99, 4.25] < (4.25, 5.5] < (5.5, 6.75] < (6.75, 8.0]]
在这里,2.999
被截断为 2.99
,因为我们将 precision
设置为 2
。
指定重复项
默认情况下,bin 边必须是唯一的,否则将引发错误。例如:
x = [3,6,8,7,3,5]
pd.qcut(x, q=5) # duplicates="raise"
ValueError: Bin edges must be unique: array([ 3., 3., 5., 6., 7., 8.]).
在这里,我们最终得到了值为 3 的两个 bin 边,因此这就是我们收到错误的原因。
为了删除(删除)多余的 bin 边,请设置 duplicates="drop"
,如下所示:
x = [3,6,8,7,3,5]
pd.qcut(x, q=5, duplicates="drop")
[(2.999, 5.0], (5.0, 6.0], (7.0, 8.0], (6.0, 7.0], (2.999, 5.0], (2.999, 5.0]]
Categories (4, interval[float64]): [(2.999, 5.0] < (5.0, 6.0] < (6.0, 7.0] < (7.0, 8.0]]
相关用法
- Python queue.PriorityQueue用法及代码示例
- Python NumPy quantile方法用法及代码示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代码示例
- Python NumPy fliplr方法用法及代码示例
- Python torch.distributed.rpc.rpc_async用法及代码示例
- Python torch.nn.InstanceNorm3d用法及代码示例
- Python sklearn.cluster.MiniBatchKMeans用法及代码示例
- Python pandas.arrays.IntervalArray.is_empty用法及代码示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代码示例
- Python numpy.less()用法及代码示例
- Python Matplotlib.figure.Figure.add_gridspec()用法及代码示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代码示例
- Python Django File.save用法及代码示例
- Python NumPy squeeze方法用法及代码示例
- Python Sympy Permutation.list()用法及代码示例
- Python dask.dataframe.Series.apply用法及代码示例
- Python networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra_path用法及代码示例
- Python scipy.ndimage.binary_opening用法及代码示例
- Python NumPy char find方法用法及代码示例
- Python pyspark.pandas.Series.dropna用法及代码示例
- Python torchaudio.transforms.Fade用法及代码示例
- Python dask.dataframe.to_records用法及代码示例
- Python arcgis.gis._impl._profile.ProfileManager.save_as用法及代码示例
- Python pyspark.pandas.groupby.SeriesGroupBy.unique用法及代码示例
- Python distributed.protocol.serialize.register_generic用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas | qcut method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。