सबसे पहले, आप सबकुछ को लोअरकेस में परिवर्तित करना, विराम चिह्न और सफेद जगह को हटाने और फिर परिणाम को शब्दों के एक समूह में परिवर्तित करना चाहते हैं।
import string
df['words'] = [set(words) for words in
df['col_name']
.str.lower()
.str.replace('[{0}]*'.format(string.punctuation), '')
.str.strip()
.str.split()
]
>>> df
col_name words
0 This is Donald. {this, is, donald}
1 His hands are so small {small, his, so, are, hands}
2 Why are his fingers so short? {short, fingers, his, so, are, why}
अब आप यह देखने के लिए बूलियन इंडेक्सिंग का उपयोग कर सकते हैं कि आपके सभी लक्षित शब्द इन नए शब्द सेट में हैं या नहीं।
target_words = ['is', 'small']
# Convert target words to lower case just to be safe.
target_words = [word.lower() for word in target_words]
df['match'] = df.words.apply(lambda words: all(target_word in words
for target_word in target_words))
print(df)
# Output:
# col_name words match
# 0 This is Donald. {this, is, donald} False
# 1 His hands are so small {small, his, so, are, hands} False
# 2 Why are his fingers so short? {short, fingers, his, so, are, why} False
target_words = ['so', 'small']
target_words = [word.lower() for word in target_words]
df['match'] = df.words.apply(lambda words: all(target_word in words
for target_word in target_words))
print(df)
# Output:
# Output:
# col_name words match
# 0 This is Donald. {this, is, donald} False
# 1 His hands are so small {small, his, so, are, hands} True
# 2 Why are his fingers so short? {short, fingers, his, so, are, why} False
निकालने के लिए मिलान पंक्तियाँ:
df.loc[[all(target_word in word_set for target_word in target_words)
for word_set in (set(words) for words in
df['col_name']
.str.lower()
.str.replace('[{0}]*'.format(string.punctuation), '')
.str.strip()
.str.split())], :]
धन्यवाद। मैं आपके बिंदु/बी -> \ b परिलक्षित करता हूं। फिर भी पूरे शब्द को पकड़ने का कोई और तरीका देखने के लिए कुछ और दिनों का इंतजार करना चाहते हैं। – Aaron
संयोग से, मुझे इसे काम करने के लिए स्ट्रिंग से पहले 'आर' जोड़ना पड़ा: किसी को पता है क्यों? मुझे इसका कोई संदर्भ नहीं मिला है .. – mccc
अच्छी तरह से, स्पष्ट रूप से '|' char इसे एक रेगेक्स में स्पष्ट रूप से बनाता है, जबकि '\ b' नहीं .. – mccc