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)
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)