python - SAS中的proc格式相当于python
问题描述
我想
proc format;
value RNG
low - 24 = '1'
24< - 35 = '2'
35< - 44 = '3'
44< - high ='4'
我在 python 熊猫中需要这个。
解决方案
如果您正在寻找等效的映射功能,您可以使用类似的东西。
df = pd.DataFrame(np.random.randint(100,size=5), columns=['score'])
print(df)
输出:
score
0 73
1 90
2 83
3 40
4 76
现在让我们对数据框中的score
列应用分箱功能,并在同一数据框中创建新列。
def format_fn(x):
if x < 24:
return '1'
elif x <35:
return '2'
elif x< 44:
return '3'
else:
return '4'
df['binned_score']=df['score'].apply(format_fn)
print(df)
输出:
score binned_score
0 73 4
1 90 4
2 83 4
3 40 3
4 76 4
推荐阅读
- excel - VBA将excel中的不同字段传输到一个没有表格的word文档中
- r - 如何将 R Markdown 输出与以前的版本进行比较?
- apache - “httpd.service 的作业失败,因为控制进程以错误代码退出。” 我该如何解决?
- elasticsearch - Elasticsearch 6 中一个索引的多个映射
- java - 比较段落中的某些字符串(句子的一部分)
- tsql - 我可以在 SQL Server 2016 中使用函数或分析函数的列上创建索引吗?
- php - 无法解析宅基地内的主机本地站点
- angular - 如何在表单组的字段中发送图像?
- html - 如何覆盖作为列方向弹性容器一部分的一个元素的列方向?
- javascript - 是否可以使用 jQuery 过滤已经获取的数据