python - Pandas qcut 适用于 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]]
解决方案
创建 bin 时只需使用 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]]
推荐阅读
- node.js - 将 nodemailer 与不同的电子邮件提供商一起使用
- javascript - 角度材质表数据源未呈现
- database - Observable 不调用 onComplete (sqlbrite - mapToOneOrDefault)
- javascript - NodeJS - socket.on 似乎没有被调用
- python - Python - 填补列表中的空白
- javascript - setState():不要直接改变状态。使用 setState()
- bash - 如何在bash中将时间戳转换为不同的时区?
- timer - STM32定时器中断周期
- php - 如何设计一个多对多关系的模式,其中一张表用于存储已订购的项目?
- javascript - requestAnimationFrame 回调中的时间戳没有变化