python - Python将不相等的数据帧与真/假文本进行比较以获取列输出
问题描述
我有以下两个数据框
df1
Animal Categ_Class
--------------------------
Cat Soft
Dog Soft
Dinosaur Hard
df2
Text Animal_Exist
-----------------------------------------------
The Cat is purring True
Cat drank the milk True
Lizard is crawling over the wall False
The dinosaurs are extinct now True
df2 中的列派生自 df2.Text 中存在的 df1.Animal
我需要帮助来理解要编写的代码,我可以得到这样的输出
输出
Text Animal_Exist Categ_Class
--------------------------------------------------------------
The Cat is purring True Soft
Cat drank the milk True Soft
Lizard is crawling over the wall False NA
The dinosaurs are extinct now True Hard
我是 python 新手,从几天以来一直在尝试多种方式。任何帮助表示赞赏。
问候。
解决方案
用于Series.str.extract
获取Animal
转换为小写的值,然后使用Series.map
import re
s = df1.assign(Animal = df1['Animal'].str.lower()).set_index('Animal')['Categ_Class']
pat = f'({"|".join(s.index)})'
cat = df2['Text'].str.extract(pat, expand=False, flags=re.I).str.lower().map(s)
df2 = df2.assign(Animal_Exist = cat.notna(), Categ_Class = cat)
print (df2)
Text Animal_Exist Categ_Class
0 The Cat is purring True Soft
1 Cat drank the milk True Soft
2 Lizard is crawling over the wall False NaN
3 The dinosaurs are extinct now True Hard
推荐阅读
- c++ - 架构 x86_64 的未定义符号:链接器错误
- iis - iis url 重写有或没有查询
- node.js - POST 在 ProxyPass 之后变为 GET
- android - Ionic Capacitor firebase 推送通知,错误:此过程中未初始化默认 FirebaseApp
- javascript - 如何从谷歌表格制作依赖下拉列表?
- unity3d - Unity 2d 平台游戏:我的代码有问题
- javascript - Angular 材质 Snackbar 配置与自定义 panelClass 配置,用于错误、成功、警告消息
- loopback4 - 绑定自定义服务时出现 Loopback 4 身份验证错误
- python - python regex:在给定字符串之后获取下一行中的字符串
- c# - 在 SolidWorks 中获取插件实例句柄 (SetAddinCallbackInfo2)