- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Pandas docs关于 qcut 函数有这样的说法:
Discretize variable into equal-sized buckets based on rank or based on sample quantiles.
import numpy as np
import pandas as pd
np.random.seed(4242)
y = pd.Series(np.random.randint(low=1, high=10, size=40))
quartiles = pd.qcut(y, 4, labels=['1st', '2nd', '3rd', '4th'])
print('Quartiles:')
print(quartiles.value_counts(sort=False))
y.groupby(quartiles).agg(['count', 'mean']).plot(kind='bar');
Quartiles:
1st 14
2nd 6
3rd 11
4th 9
dtype: int64
最佳答案
发生这种情况的原因是因为 python 不知道如何处理“边界线”情况,即可能很容易满足第一和第二四分位数的记录。对您的代码进行简单的调整将产生所需的结果:
import numpy as np
import pandas as pd
np.random.seed(4242)
y = pd.Series(np.random.randint(low=1, high=10, size=40))
quartiles = pd.qcut(y.rank(method = 'first'), 4, labels=['1st', '2nd', '3rd', '4th'])
print('Quartiles:')
print(quartiles.value_counts(sort=False))
y.groupby(quartiles).agg(['count', 'mean']).plot(kind='bar');
通过使用
rank()
说明 python 使用的方法函数,我们为 python 提供了一种处理跨越多个 bin 的记录的清晰方法。在这种情况下,我使用了
(method = 'first')
作为
rank()
的参数功能。
Quartiles:
1st 10
2nd 10
3rd 10
4th 10
dtype: int64
关于python - 为什么 Pandas qcut 给我大小不等的垃圾箱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44636160/
假设我有一个数据框: import numpy as np import pandas as pd df = pd.DataFrame(np.random.normal(0,1,[100,50]))
我想将 pandas qcut 应用于滚动窗口。我不知道该怎么做...想法是在过去 20 天中,找到属于上四分位数的值,找到上四分位数中的值的平均值。并返回该滚动时间序列的平均值。 如果我有 s =
这是一个简单的数据样本系列: sample Out[2]: 0 0.047515 1 0.026392 2 0.024652 3 0.022854 4 0.020397
我创建了一个分类变量,我想为其他变量的特定值创建一个新类别 我有一个带有变量 Score 的数据框,其值介于 0-100 之间。我做了十分之一,但我想为特定值创建一个新类别 df['Score_pr'
考虑以下数据: s = pd.Series([1, 1, 1, 2]) pd.qcut(s, 2, labels=False) 我想要等概率组,即 [0, 0, 1, 1]。相反,qcut 可以引发异
在此处的 Pandas 笔记本上 http://nbviewer.ipython.org/urls/raw.github.com/carljv/Will_it_Python/master/ARM/ch
我有一个数据框,我可以从中选择一个列(系列),如下所示: df: value_rank 275488 90 275490 35 275491
Pandas docs关于 qcut 函数有这样的说法: Discretize variable into equal-sized buckets based on rank or based on
我使用 pandas.qcut 将数据分为 5 组,并希望根据 qcut 的最小和最大分数来标记每个组。 例如,我尝试使用数据框列中的“年龄”数据。 df['age group'] = pd.qcut
我正在慢慢地从 R 转向 python + pandas,我面临着一个我无法解决的问题...... 我需要离散化一列中的值,方法是将它们分配给 bin,并将具有这些 bin 名称的列添加到原始 Dat
假设我有一个列表: a = [3, 5, 1, 1, 3, 2, 4, 1, 6, 4, 8] 和一个子列表: b = [5, 2, 6, 8] 我想通过 pd.qcut(a,2) 获取 bin 并计
有没有一种方法可以构造 Pandas groupby 和 qcut 命令以返回具有嵌套图 block 的一列?具体来说,假设我有 2 组数据,我希望将 qcut 应用于每组,然后将输出返回到一列。这类
我的问题和上一个一样: Binning with zero values in pandas 但是,我仍然想在分位数中包含 0 值。有没有办法做到这一点?换句话说,如果我有 600 个值,其中 50%
有两个 ndarray: import pandas as pd import numpy as np a = np.arange(0,100, 10) b = np.random.random_in
我对 groupby 结果应用 qcut,以下是我的问题的简化版本: a = pd.DataFrame({'A':[1,1,1,1,2,2,2,2], 'B': [
我正在做 Recency-Frequency-Monetary 分析,虽然我有一个在 Python 中工作的模型,但由于生产代码主要是 PHP(Oracle 12c fwiw 或者也可以在 postg
我在名为example 的对象的列中有很多分数。我想将这些分数分成十分位数,并为每一行分配相应的十分位数间隔。我尝试了以下方法: import random import pandas as pd r
更新:从版本 0.20.0 开始,pandas cut/qcut 确实可以处理日期字段。参见 What's New了解更多。 pd.cut and pd.qcut now support dateti
我正在使用 pandas qcut 将一些数据分成 20 个 bin,作为数据准备的一部分,用于训练二进制分类模型,如下所示: data['VAR_BIN'] = pd.qcut(cc_data[va
我有一个包含 499 条记录的埃博拉数据集。我试图根据概率(概率变量)找到每个五分位数中的观察次数。观察次数应分为 0-20%、20-40% 等类别。我认为这样做的代码是, test = pd.qcu
我是一名优秀的程序员,十分优秀!