Python 信用风险建模
Michael Crabtree
Data Scientist, Ford Motor Company
''| 缺失值类型 | 可能结果 |
|---|---|
| 数值列中的 NULL | 错误 |
| 字符串列中的 NULL | 错误 |
| 缺失项 | 解释 | 处理 |
|---|---|---|
loan_status 为 NULL |
贷款刚获批 | 从预测数据中移除 |
person_age 为 NULL |
未记录/未披露年龄 | 用中位数替换 |
isnull() 可轻松查找空值sum() 可统计空值数量.any() 方法检查所有列null_columns = cr_loan.columns[cr_loan.isnull().any()]
cr_loan[null_columns].isnull().sum()
# 各列空值总数
person_home_ownership 25
person_emp_length 895
loan_intent 25
loan_int_rate 3140
cb_person_default_on_file 15
.fillna() 结合聚合函数替换缺失值cr_loan['loan_int_rate'].fillna((cr_loan['loan_int_rate'].mean()), inplace = True)
.drop() 直接删除这些记录indices = cr_loan[cr_loan['person_emp_length'].isnull()].index
cr_loan.drop(indices, inplace=True)
Python 信用风险建模