1 回答
TA贡献1853条经验 获得超18个赞
您可以将文本拆分为单词列表,然后可以使用isin()
import pandas as pd
df = pd.DataFrame({
'CODE': ['excel', 'apple'],
'RESULT':['OK. Excel', 'OK. Apple']
})
text = 'I want to receive an apple'.lower()
words = text.split(' ')
mask = df['CODE'].isin(words)
print( mask )
print( df[ mask ] )
if mask.any():
name = df[mask]['RESULT'].iloc[0]
else:
name = None
print('Name:', name)
结果
# mask
0 False
1 True
Name: CODE, dtype: bool
# df[mask]
CODE RESULT
1 apple OK. Apple
Name: OK. Apple
编辑:其他方法
mask = df['CODE'].apply(lambda x: x.lower() in text.lower())
法典
import pandas as pd
df = pd.DataFrame({
'CODE': ['excel file', 'apple'],
'RESULT':['OK. Excel', 'OK. Apple']
})
text = 'I have excel file on the PC'
mask = df['CODE'].apply(lambda x: x.lower() in text.lower())
print( mask )
print( df[ mask ] )
if mask.any():
name = df[mask]['RESULT'].iloc[0]
else:
name = None
print('Name:', name)
结果
# mask
0 True
1 False
Name: CODE, dtype: bool
# df[mask]
CODE RESULT
0 excel file OK. Excel
Name: OK. Excel
添加回答
举报