小编ska*_*agr的帖子

为什么 Pandas qcut 给我大小不等的垃圾箱?

Pandas 文档对 qcut 函数有这样的说法:

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

所以我希望这段代码能给我 4 个箱子,每个箱子有 10 个值:

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');
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个:

Quartiles:
1st    14
2nd     6
3rd    11
4th     9
dtype: int64
Run Code Online (Sandbox Code Playgroud)

图形

我在这里做错了什么?

python pandas

7
推荐指数
1
解决办法
1242
查看次数

标签 统计

pandas ×1

python ×1