gpt4 book ai didi

python - Pandas 为什么 pd.cut() 产生负值

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:06 25 4
gpt4 key购买 nike

片段:

test = pd.DataFrame({'counts':[0,1,2,3,4,5,6,11,12,14,15]})
test['range'] = pd.cut(test.counts, [0,5,10,15], include_lowest=True)
test

输出:

    counts  range
0 0 (-0.001, 5.0]
1 1 (-0.001, 5.0]
2 2 (-0.001, 5.0]
3 3 (-0.001, 5.0]
4 4 (-0.001, 5.0]
5 5 (-0.001, 5.0]
6 6 (5.0, 10.0]
7 11 (10.0, 15.0]
8 12 (10.0, 15.0]
9 14 (10.0, 15.0]
10 15 (10.0, 15.0]

我可以得到 (0, 5.0] 而不是 (-0.001, 5.0] 吗?为什么即使我没有指定它也会出现 -0.001?

最佳答案

这是 the result of include_lowest=True internal logic .

您可以自己生成标签,方法与 pd.cuts()include_lowest=False 时执行的方式相同:

In [50]: import pandas.core.algorithms as algos

In [51]: labels = pd.Categorical(pd.core.reshape.tile._format_labels(algos.unique(bins), precision=0),
ordered=True)

In [52]: labels
Out[52]:
[(0, 5], (5, 10], (10, 15]]
Categories (3, interval[int64]): [(0, 5] < (5, 10] < (10, 15]]

In [53]: test['range'] = pd.cut(test.counts, [0,5,10,15],
labels=labels,
include_lowest=True)

In [54]: test
Out[54]:
counts range
0 0 (0, 5]
1 1 (0, 5]
2 2 (0, 5]
3 3 (0, 5]
4 4 (0, 5]
5 5 (0, 5]
6 6 (5, 10]
7 11 (10, 15]
8 12 (10, 15]
9 14 (10, 15]
10 15 (10, 15]

关于python - Pandas 为什么 pd.cut() 产生负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46021475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com