python - 在 qcut decile 之后选择/过滤 bins

标签 python pandas quantile binning

在按十分位数对数据进行分箱后,我正在尝试访问标签(即位置指示器):

q = pd.qcut(df["revenue"], 10)


q.head():

7     (317.942, 500.424]
81    (317.942, 500.424]
83     (150.65, 317.942]
84        [0.19, 150.65]
85    (317.942, 500.424]
Name: revenue, dtype: category
    Categories (10, object): [[0.19, 150.65] < (150.65, 317.942] < (317.942, 500.424] < (500.424, 734.916] ... (1268.306, 1648.35] 
< (1648.35, 1968.758] < (1968.758, 2527.675] < (2527.675, 18690.2]]
    In [233]:

此帖link显示您可以执行以下操作来访问标签:

>>> q.labels

但是当我这样做时,我得到:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-246-e806c96b1ab2> in <module>()
----> 1 q.labels

C:\Users\blah\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   2666         if (name in self._internal_names_set or name in self._metadata or
   2667                 name in self._accessors):
-> 2668             return object.__getattribute__(self, name)
   2669         else:
   2670             if name in self._info_axis:

AttributeError: 'Series' object has no attribute 'labels'

无论如何,我想做的是使用标签来过滤我的数据 - 可能是通过在 df 中添加一个新列来表示十分位数(或分位数)结果的位置标签。

最佳答案

我个人喜欢在 pd.qcut 中使用 labels 参数来指定外观简洁且一致的标签。

np.random.seed([3,1415])
df = pd.DataFrame(dict(revenue=np.random.randint(1000000, 99999999, 100)))
df['decile'] = pd.qcut(df.revenue, 10, labels=range(10))
print(df.head())

正如@jeremycg 所指出的,您可以通过cat 访问器属性访问类别信息

df.decile.cat.categories

Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')

可以快速描述每个bin

df.groupby('decile').describe().unstack()

enter image description here


可以过滤

df.query('decile >= 8')

     revenue decile
4   98274570      9
6   99418302      9
19  89598752      8
20  88877661      8
22  90789485      9
29  83126518      8
31  90700517      9
33  96816407      9
40  89937348      8
54  83041116      8
65  83399066      8
66  97055576      9
79  87700403      8
81  88592657      8
82  91963755      9
83  82443566      8
84  84880509      8
88  98603752      9
95  92548497      9
98  98963891      9

您可以在十分位数内进行 z 得分

df = df.join(df.groupby('decile').revenue.agg(dict(Mean='mean', Std='std')), on='decile')
df['revenue_zscore_by_decile'] = df.revenue.sub(df.Mean).div(df.Std)
df.head()

    revenue decile           Std      Mean  revenue_zscore_by_decile
0  32951600      2  2.503325e+06  29649669                  1.319018
1  70565451      6  9.639336e+05  71677761                 -1.153928
2   6602402      0  5.395453e+06  11166286                 -0.845876
3  82040251      7  2.976992e+06  78299299                  1.256621
4  98274570      9  3.578865e+06  95513475                  0.771500

关于python - 在 qcut decile 之后选择/过滤 bins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41354611/

相关文章:

python - pandas.read_csv 来自字符串或包数据

python - Pandas:删除数据框中的重复行

python - 使用多核的 Pip 构建选项

python - 从 Python 将 null 插入 Sqlite

python - 从字典中随机删除项目

python - 如何在 Python 中将不平衡列表添加到数据帧中?

可靠地检索分位数函数的逆函数

r - 如何让分位数与 summarise_at 和 group_by (dplyr) 一起使用

python - 如何在 Windows 中的 python 3 中正确地将焦点传递给消息框或从消息框传递焦点?