我正在搜索400萬行數據框中的子字符串或多個子字符串。如何使熊貓數據框str.contains搜索更快
df[df.col.str.contains('Donald',case=True,na=False)]
或
df[df.col.str.contains('Donald|Trump|Dump',case=True,na=False)]
數據幀(DF)看起來像以下(400萬個行)
df = pd.DataFrame({'col': ["very definition of the American success story, continually setting the standards of excellence in business, real estate and entertainment.",
"The myriad vulgarities of Donald Trump—examples of which are retailed daily on Web sites and front pages these days—are not news to those of us who have",
"While a fearful nation watched the terrorists attack again, striking the cafés of Paris and the conference rooms of San Bernardino"]})
是否有任何提示,使這個字符串搜索速度更快? 例如,首先排序數據框,以某種方式建立索引,將列名更改爲數字,從查詢中刪除「na = False」等。即使毫秒級的速度增加也會非常有幫助!