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
添加回答
舉報