gpt4 book ai didi

python - Pandas 切割方法排除下限

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:50 24 4
gpt4 key购买 nike

我正在尝试对包含 0 到 100 范围内的年龄的数据框列进行分类。当我尝试使用 bin 来包含零年龄时,它不起作用。

这是一个使用包含我的数据范围的列表的演示:

pd.cut(pd.Series(range(101)), [0, 24, 49, 74, 100])

范围内的零值从切割中返回 NaN。

有什么办法解决这个问题吗?

最佳答案

IIUC 您需要将 include_lowest 参数设置为 True。来自 docs :

include_lowest : bool
Whether the first interval should be left-inclusive or not.

针对您的情况:

pd.cut(pd.Series(range(101)), [0,24,49,74,100], include_lowest=True)

In [148]: pd.cut(pd.Series(range(101)), [0,24,49,74,100], include_lowest=True).head(10)
Out[148]:
0 [0, 24]
1 [0, 24]
2 [0, 24]
3 [0, 24]
4 [0, 24]
5 [0, 24]
6 [0, 24]
7 [0, 24]
8 [0, 24]
9 [0, 24]
dtype: category
Categories (4, object): [[0, 24] < (24, 49] < (49, 74] < (74, 100]]

关于python - Pandas 切割方法排除下限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35763698/

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