pandas.cut 函数在假定为 0 时给了我负值

发布于 2025-01-16 19:41:39 字数 349 浏览 2 评论 0原文

我很困惑为什么我的 pd.cut 函数给我的起始间隔是负值。 我已剪切的列的最小值为 0。因此,我希望 pd.cut 函数抛出的第一个区间为 (0,18),而不是 (-0.18,18)。

我已将精度设置更改为 0。但是,这只会使我的起始间隔变为 (-0.0,18)。

当我解析到 pd.cut 函数中的列是整数时,为什么我的间隔全部是浮点数?

这是我的作品图片

感谢所有帮助。谢谢。

I am perplexed as to why my pd.cut function gave me the starting interval that is a negative value.
The column that I have cut on, has a minimum value of 0. Hence, I expect my pd.cut function to throw out my first interval to be (0,18) instead of (-0.18,18).

I have changed the precision setting to be 0. However, that just makes my starting interval to be (-0.0,18).

And why is my intervals all in float when the column I've parsed into my pd.cut function is in integers?

This is a picture of my work

Would appreciate all help. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2025-01-23 19:41:39

正如评论中所解释的,您要求 cut 自动为您定义垃圾箱,默认情况下它们是等宽的,这意味着可以有负边界。

如果您希望保留自动分箱,您可以随后手动修改间隔。这是仅第一个间隔“不正确”的情况的示例,使用 cat.rename_categories

np.random.seed(0)
s = pd.Series(np.random.randint(-10,100,size=100)).clip(lower=0)
s_cut = pd.cut(s, bins=10)
print(s_cut.cat.categories)

first_I = s_cut.cat.categories[0]
new_I = pd.Interval(0, first_I.right)
s_cut = s_cut.cat.rename_categories({first_I: new_I})
print(s_cut.cat.categories)

输出:

# before
IntervalIndex([(-0.095, 9.5], (9.5, 19.0], (19.0, 28.5], ...)

# after
IntervalIndex([(0.0, 9.5], (9.5, 19.0], (19.0, 28.5], ...)

As explained in the comments, you asked cut to define the bins automatically for you, by default they are equal width, which mean having a negative bound is possible.

If you wish to keep the automatic binning, you can modify the intervals manually afterwards. Here is an example in case of only the first interval that is "incorrect", using cat.rename_categories:

np.random.seed(0)
s = pd.Series(np.random.randint(-10,100,size=100)).clip(lower=0)
s_cut = pd.cut(s, bins=10)
print(s_cut.cat.categories)

first_I = s_cut.cat.categories[0]
new_I = pd.Interval(0, first_I.right)
s_cut = s_cut.cat.rename_categories({first_I: new_I})
print(s_cut.cat.categories)

output:

# before
IntervalIndex([(-0.095, 9.5], (9.5, 19.0], (19.0, 28.5], ...)

# after
IntervalIndex([(0.0, 9.5], (9.5, 19.0], (19.0, 28.5], ...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文