数据框另一列中一列的搜索值

时间:2020-06-16 10:04:21

标签: python dataframe find

title列中有诸如dog之类的关键字,您必须在名为Text的列的同一行中进行搜索,然后创建一个称为句子的新列,并在找到该词的位置写上短语。

import pandas as pd

PAL = {'TITLE': ['CAT','DOG','MOUSE','BIRD'],
        'TEXT': ["""
I like small dogs. 
I do not like cats. Birds fly through the sky. 
A mouse eat cheese and they are small""","""
I like small dogs. 
I do not like cats. 
Birds fly through the sky. A mouse eat cheese and they are small""","""
I like small dogs. 
I do not like cats. Birds fly through the sky. 
A mouse eat cheese and they are small""","""
I like small dogs. 
I do not like cats. Birds fly through the sky. 
A mouse eat cheese and they are small"""]
        }

df = pd.DataFrame(PAL, columns = ['TITLE', 'TEXT'])

print (df)

enter image description here

import pandas as pd

PAL = {'TITLE': ['CAT','DOG','MOUSE','BIRD'],
        'TEXT': ["""
I like small dogs. 
I do not like cats. Birds fly through the sky. 
A mouse eat cheese and they are small""","""
I like small dogs. 
I do not like cats. 
Birds fly through the sky. A mouse eat cheese and they are small""","""
I like small dogs. 
I do not like cats. Birds fly through the sky. 
A mouse eat cheese and they are small""","""
I like small dogs. 
I do not like cats. Birds fly through the sky. 
A mouse eat cheese and they are small"""],
     'SENTENCE':["I do not like cats","I like small dogs","A mouse eat cheese and they are small","Birds fly through the sky"]   }

df = pd.DataFrame(PAL, columns = ['TITLE', 'TEXT','SENTENCE'])

print (df)

enter image description here

0 个答案:

没有答案