我正在尝试将包含0到100范围内的年龄的数据帧列绑定.当我尝试使用bin来包含零年龄时,它不起作用.
这是一个使用包含我的数据范围的列表的演示:
pd.cut(pd.Series(range(101)), [0, 24, 49, 74, 100])
Run Code Online (Sandbox Code Playgroud)
范围中的零值从切割返回NaN.
有什么方法吗?
IIUC你需要设置include_lowest参数True.来自docs:
include_lowest:bool
第一个区间是否应该是包含左对齐的.
对于你的情况:
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]]
Run Code Online (Sandbox Code Playgroud)