簡體   English   中英

在 dataframe 中使用 str.contains 和正則表達式搜索單詞很慢,有沒有更好的方法?

[英]Searching for words using str.contains and regex in dataframe is slow, is there a better way?

我有一個超過 200 萬行的數據庫。 我正在嘗試使用正則表達式查找包含兩個單詞的行,例如:

df1 = df[df['my_column'].str.contains(r'(?=.*first_word)(?=.*second_word)')]

但是,當嘗試在 jupyter notebook 中處理這個問題時,返回這些行需要一分鍾多的時間,或者它會使內核崩潰,我必須再試一次。

有沒有更有效的方法來返回包含兩個單詞的 dataframe 中的行?

利用

df['my_column'].apply(lambda x: all(l in x for l in ['first_word', 'second_word']) )

它將確保列表中的單詞都出現在my_column列中,而沒有尷尬的正則表達式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM