Python将不相等的数据帧与true/false中的文本进行比较,以获得列输出

Python将不相等的数据帧与true/false中的文本进行比较,以获得列输出,python,pandas,Python,Pandas,我有以下两个数据帧 df1 df2 df2中的列源自df2.Text中存在的df1.Animal 我需要帮助理解要编写的代码,这样我才能得到这样的输出 输出 Text Animal_Exist Categ_Class -------------------------------------------------------------- The Cat is purring True

我有以下两个数据帧

df1

df2

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新手,从几天以来一直在尝试多种方法。 感谢您的帮助

注意。

用于将
动物的值转换为小写,然后使用


当然,我会记住这一点。很抱歉给您带来不便。我试图把它作为文本,但表格变得不可读。您能告诉我如何在stackoverflow中发布这种类型的表吗。感谢您的帮助。因此,请将图片转换为文本版本的数据。请您指导我如何以文本形式显示表格格式的数据。。我在拍照前先试过,但看不懂。。请导游。试试看,我可以帮你。只能粘贴,我可以在粘贴后编辑。@jezrael按建议完成。。你能帮我把它格式化吗?评论不是用来讨论的;这段对话已经结束。
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
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
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