我很難將 DataFrame 列中的字串與字串串列進行比較。
讓我向你解釋:我從社交媒體收集了一個個人專案的資料,除此之外,我創建了一個如下所示的字串串列:
the_list = ['AI', 'NLP', 'approach', 'AR Cloud', 'Army_Intelligence', 'Artificial general intelligence', 'Artificial tissue', 'artificial_insemination', 'artificial_intelligence', 'augmented intelligence', 'augmented reality', 'authentification', 'automaton', 'Autonomous driving', 'Autonomous vehicles', 'bidirectional brain-machine interfaces', 'Biodegradable', 'biodegradable', 'Biotech', 'biotech', 'biotechnology', 'BMI', 'BMIs', 'body_mass_index', 'bourdon', 'Bradypus_tridactylus', 'cognitive computing', 'commercial UAVs', 'Composite AI', 'connected home', 'conversational systems', 'conversational user interfaces', 'dawdler', 'Decentralized web', 'Deep fakes', 'Deep learning', 'defrayal']
還有其他詞,但這只是給你一個想法。
我的目標是將這個串列中的每個單詞與包含標題和帖子訊息(來自 reddit)的 2 個現有 DF 列進行比較。明確地說,我想創建一個新列,在其中顯示在我的串列與包含帖子的列之間匹配的單詞。
到目前為止,這就是我所做的:
the_list = ['AI', 'NLP', 'approach', 'AR Cloud', 'Army_Intelligence', 'Artificial general intelligence', 'Artificial tissue', 'artificial_insemination', 'artificial_intelligence', 'augmented intelligence', 'augmented reality', 'authentification', 'automaton', 'Autonomous driving', 'Autonomous vehicles', 'bidirectional brain-machine interfaces', 'Biodegradable', 'biodegradable', 'Biotech', 'biotech', 'biotechnology', 'BMI', 'BMIs', 'body_mass_index', 'bourdon', 'Bradypus_tridactylus', 'cognitive computing', 'commercial UAVs', 'Composite AI', 'connected home', 'conversational systems', 'conversational user interfaces', 'dawdler', 'Decentralized web', 'Deep fakes', 'Deep learning', 'defrayal']
df['matched text'] = df.text_lemmatized.str.extract('({0})'.format('|'.join(the_list)), flags = re.IGNORECASE)
df = df[~pd.isna(df['matched text'])]
df
>>Outpout:
title_lemmatized text_lemmatized matched_word(s)
0 Title1 'claim thorough vet...' 'ai'
1 Title@ 'Yeaaah today iota...' 'IoT'
這里是輸出結果的更多細節。
問題:主要問題是它回傳與串列匹配的字母(不是實際單詞)。
例子:
--> the_list = 'ai'(人工智能)或 IoT(物聯網)
--> df['text_lemmatized'] 在文本中有'claim'這個詞,那么'ai'將是匹配項。或“Iota”將與“IoT”匹配。
我的愿望:
title_lemmatized text_lemmatized matched_word(s)
0 Title1 'AI claim that Iot devises...' 'AI', 'IoT'
1 Title2 'The claim story about...'
2 Title3 'augmented reality and ai are...' 'augmented reality', 'ai'
3 Title4 'AI ai or artificial intelligence' 'AI', 'ai', 'artificial intelligence'
非常感謝:)
uj5u.com熱心網友回復:
您必須'\b'在正則運算式模式中添加單詞邊界。從re 模塊檔案:
\b匹配空字串,但僅在單詞的開頭或結尾。一個詞被定義為一個詞字符序列。請注意,形式上,\b 被定義為 \w 和 \W 字符之間(或反之亦然),或 \w 和字串開頭/結尾之間的邊界。這意味著 r'\bfoo\b' 匹配 'foo', 'foo.', '(foo)', 'bar foo baz' 但不匹配 'foobar' 或 'foo3'。
除此之外,您想使用Series.str.findall(或Series.str.extractall) 而不是Series.str.extract查找所有匹配項。
這應該作業
the_list = ['AI', 'NLP', 'approach', 'AR Cloud', 'Army_Intelligence', 'Artificial general intelligence', 'Artificial tissue', 'artificial_insemination', 'artificial_intelligence', 'augmented intelligence', 'augmented reality', 'authentification', 'automaton', 'Autonomous driving', 'Autonomous vehicles', 'bidirectional brain-machine interfaces', 'Biodegradable', 'biodegradable', 'Biotech', 'biotech', 'biotechnology', 'BMI', 'BMIs', 'body_mass_index', 'bourdon', 'Bradypus_tridactylus', 'cognitive computing', 'commercial UAVs', 'Composite AI', 'connected home', 'conversational systems', 'conversational user interfaces', 'dawdler', 'Decentralized web', 'Deep fakes', 'Deep learning', 'defrayal']
pat = r'\b({0})\b'.format('|'.join(the_list))
df['matched text'] = df.text_lemmatized.str.findall(pat, flags = re.IGNORECASE).map(", ".join)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/343396.html
上一篇:Pandas-回圈作業表
