python - 根据字符串在新列中添加值包含在另一列中
问题描述
我有 DataFarame
date descriptions Code
1. 1/1/2020 this is aPple 6546
2. 21/8/2019 this is fan for him 4478
3. 15/3/2020 this is ball of hockey 5577
4. 12/2/2018 this is Green apple 7899
5. 13/3/2002 this is iron fan 7788
6. 14/5/2020 this ball is soft 9991
我想创建一个新列'category',其值将是如果描述列中有表达式apple,fan,ball(大写或小写字母),那么值A001,F009,B099应该分别输入到类别列中,必填DataFrame 将是。
date descriptions Code category
1. 1/1/2020 this is aPple 6546 A001
2. 21/8/2019 this is fan for him 4478 F009
3. 15/3/2020 this is ball of hockey 5577 B099
4. 12/2/2018 this is Green apple 7899 A001
5. 13/3/2002 this is iron fan 7788 F009
6. 14/5/2020 this ball is soft 9991 B099
解决方案
用于str.extract
从基于字符串的列中获取子字符串
d = {'apple': 'A001', 'ball': 'B099', 'fan': 'F009'}
df['category'] = (
df.descriptions
.str.lower()
.str.extract('(' + '|'.join(d.keys()) + ')')
.squeeze().map(d)
)
推荐阅读
- jquery - jQuery DataTables 添加动态列
- python - 如何自定义带有复数的 DataFrame 的显示格式?
- php - Wordpress cronjob 无法正常工作
- node.js - Using Express-Handlebars, how can I set an Item to active from my Navigation Bar?
- javascript - 如何自动将元素聚焦在滚动上?
- assembly - rax/rdi 不会延续到下一个系统调用?
- typescript - 组内的导入源必须按字母顺序排列
- tensorflow - Django 开发服务器中的 TensorFlow 预测问题(带线程)
- asterisk - VtigerAsteriskConnector,传入 AGI 失败“文件过早结束”
- python - 在熊猫数据框中搜索不需要的值