python - 如何将数字转换为熊猫列中的类别
解决方案
您可以使用numpy.select
:
In [744]: df
Out[744]:
A
0 12
1 23
2 18
3 39
4 15
In [744]: import numpy as np
In [745]: conditions = [df.A.le(20), df.A.gt(20)]
In [746]: choices = ['Less than or equal to 20', 'Greater than 20']
In [749]: df['categorical_A'] = np.select(conditions, choices)
In [750]: df
Out[750]:
A categorical_A
0 12 Less than or equal to 20
1 23 Greater than 20
2 18 Less than or equal to 20
3 39 Greater than 20
4 15 Less than or equal to 20
推荐阅读
- vue.js - 尽管赋值成功,vue 变量未定义
- sql - 通过多个连接获得正确的总和
- html - 无法让引导程序响应工作
- bash - bash to pass array to ssh to another server
- sql - SQL JOIN when empty link still show row
- python - JSON 对象必须是 str、bytes 或 bytearray,而不是“生成器”
- sql - 如何导出报告并用数据标记列?
- python-3.x - How to partial fill_between in matplotlib, as in different colors for different values
- angular - 数据如何从 html 文件传输到 component.ts 文件
- reactjs - 主题黑暗将文本字段变为白色