![](/img/trans.png)
[英]exact and case insensitive match for a multi word token in a string python
[英]Return substring if present in a string and match with case insensitive python
如果字符串中存在子字符串,我目前正在嘗試返回一個不區分大小寫的子字符串。
所以一個例子是,即使句子是“蘋果很酷”或“我喜歡蘋果”或“我喜歡蘋果”,我也想返回字符串“蘋果”
到目前為止我所擁有的是:
df_word_list = pd.DataFrame({'word': ['apple','cool']})
df= pd.DataFrame({'sentence': ['"Apple is cool","I like APPLE","I like apples"]})
words = [x for x in df_word_list['word'].tolist() if x in str(df['sentence'][i])]
這給了我返回的單詞,但它區分大小寫,有人知道如何將其變為不區分大小寫嗎?
我希望最終的輸出是
第 3 行是空的,因為它有一個“s”(“apples”而不是“apple”)
df_words_list 是我想要識別的單詞的數據框。 df 是包含句子的數據框。
df.sentence.str.lower().str.split().apply(lambda l: ", ".join([x for x in l if x in df_word_list["word"].values]))
結果是pandas.Series
字符串
0 apple, cool
1 apple
2
Name: sentence, dtype: object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.