pandas 切割法不包括下界 [英] Pandas cut method excludes lower bound
本文介绍了 pandas 切割法不包括下界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对一个包含0到100范围内的年龄的数据框列进行装箱. 当我尝试使用垃圾桶添加零龄期时,它不起作用.
I am trying to bin a dataframe column that contains ages in the range 0 to 100. When I try and use a bin to include the zero ages it does not work.
这是一个演示列表,其中包含我的数据范围:
Here is an demo using a list with the range of my data:
pd.cut(pd.Series(range(101)), [0, 24, 49, 74, 100])
该范围内的零值从切割返回NaN.
The zero value in the range returns NaN from the cut.
有什么办法解决吗?
推荐答案
IIUC,您需要将include_lowest
参数设置为True
.来自文档:
IIUC you need to set include_lowest
argument to True
. From docs:
include_lowest : bool
第一个时间间隔是否应包含在左值范围内.
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]]
这篇关于 pandas 切割法不包括下界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文