Pandas qcut 应用于 NaN 中的新数据结果

Pandas qcut apply on new data result in NaN

我正在为一个建模项目装箱,我 运行 遇到了这个问题。 此示例使用不带 11 的数据帧获取 bin,当将 bin 应用于其中包含 11 的新数据帧时,这会导致 NaN。显然这会发生,但我想知道是否有(通常有)任何聪明的方法可以轻松处理这个问题,例如将 (7.75, 10.0] 变成 (7.75, np.inf).


import pandas as pd
a,bin = pd.qcut(pd.DataFrame({"A":[1,2,3,4,5,6,7,8,9,10]}).A,retbins = True, q = 4)
pd.cut(pd.DataFrame({"A":[1,2,11]}).A, bins = bin ,include_lowest = True)


0    (0.999, 3.25]
1    (0.999, 3.25]
2              NaN
Name: A, dtype: category
Categories (4, interval[float64]): [(0.999, 3.25] < (3.25, 5.5] < (5.5, 7.75] < (7.75, 10.0]]

创建 bins 时只需使用 np.inf 而不是 10

a,bin = pd.qcut(pd.DataFrame({"A":[1,2,3,4,5,6,7,8,9,np.inf]}).A,retbins = True, q = 4)
pd.cut(pd.DataFrame({"A":[1,2,11]}).A, bins = bin ,include_lowest = True)

0    (0.999, 3.25]
1    (0.999, 3.25]
2      (7.75, inf]
Name: A, dtype: category
Categories (4, interval[float64]): [(0.999, 3.25] < (3.25, 5.5] < (5.5, 7.75] < (7.75, inf]]