Re: [問題]請教如何加快dataframe的條件判斷

看板Python作者 (天)時間11月前 (2023/05/15 01:47), 11月前編輯推噓5(503)
留言8則, 5人參與, 11月前最新討論串2/2 (看更多)
※ 引述《liquidbox (樹枝擺擺)》之銘言: : 請問,我有一個近萬個由不重複字串組成的list叫kw_list,以及一個df : 範例是['book','money','future','file'] : Index sentence : 1 This is a book : 2 back to the future : 3 replace the file : 4 come on : 5 have a nice weekend : 我想要把list中的字串逐一拉出來, : 跟sentence那個欄位比較,如果sentence欄位有包含該字串(近萬個都要逐一比對) : 就標上True,否則就False : 我建了一個近萬個column的新dataframe,欄位是kw_list : 然後跟原本的df合併起來, : 然後再寫個條件判斷式,若該筆資料的sentence包含該字串, : 那個column就標上True,不然就False : 於是會變成 : Index sentence book money future file : 1 This is a book TRUE FALSE FALSE FALSE : 2 back to the future FALSE FALSE TRUE FALSE : 3 replace the file FALSE FALSE FALSE TRUE : 4 come on FALSE FALSE FALSE FALSE : 5 have a nice weekend FALSE FALSE FALSE FALSE : 不意外地,我用迴圈去判斷,跑幾小時都跑不出結果,如下: : for kw in kw_list: : df.loc[df['sentence'].str.contains(kw),df[kw]]=True : 我覺得我把同樣的東西丟到Excel用函數算可能都比較快, : 請問有什麼方法改寫,讓這個df的運算速度加快嗎 有幾個人跟我稍微討論了一下 我這裡放上幾個方法的比較 三十萬個隨機句子 隨機抓出2389個關鍵字 三個方法的結果如下 1. polars 1.21 s ± 59.3 ms per loop (mean ± std. dev. of 5 runs, 3 loops each) 2. pandas a. Pre-allocate columns first and set values 6min 36s b. for loop add columns => PerformanceWarning 6min 16s c. Pre-allocate columns + np.where + pandas .at 7min 59s 3. duckdb 24.4 s ± 177 ms per loop (mean ± std. dev. of 2 runs, 3 loops each) 4. numpy a. pre-allocate + for-loop: 4min 23s b. pre-allocate + np.char: > 6 minutes 5. Cython 1.73 s ± 14.7 ms per loop (mean ± std. dev. of 3 runs, 5 loops each) 可以看出來polars跟2/3/4方法根本不在同一個量級 polars的with_columns在Rust底層中是會做multi-threading 另外3個都是single thread,所以根本沒法比 Cython則是有接近polars的效能,但是還是小輸,而且結果是np.array不是dataframe 如果捨棄DataFrame的操作的話 Cython+numpy有接近媲美polars的速度 但是很難寫也很難調校 我寫了三小時多才得到一個滿意的版本 附上測試程式碼: https://reurl.cc/9VMOkO 機器: AMD TR-2990WX@3.6GHz with Python 3.10.9 on Windows 11 polars version: 0.17.13 pandas version: 1.5.3 duckdb version: 0.7.1 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.239.131 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1684086439.A.CF1.html

05/15 06:42, 11月前 , 1F
謝謝分享,我再來研究這個工具
05/15 06:42, 1F

05/15 09:33, 11月前 , 2F
dask dataframe呢
05/15 09:33, 2F
dask底層是pandas 在原理上還是很難超越polars 我覺得甚至連duckdb都很難企及 我剛剛有嘗試去改寫 但找不到方法優化

05/15 09:43, 11月前 , 3F
dask還是輸polars,polars就是blazingly fast!!!
05/15 09:43, 3F

05/15 12:34, 11月前 , 4F
感謝分享 Polar真的快很多
05/15 12:34, 4F

05/15 13:28, 11月前 , 5F
Rust真神
05/15 13:28, 5F
5/15 14:53 增加pandas .at, numpy & Cython比較

05/15 15:23, 11月前 , 6F
pandas的str吃GIL polars加上to_pandas會比較公平一點
05/15 15:23, 6F

05/15 15:25, 11月前 , 7F
不過polars用lazy再collect optimize搞不好更快w
05/15 15:25, 7F
已加 沒差多少

05/15 18:59, 11月前 , 8F
長知識
05/15 18:59, 8F
※ 編輯: celestialgod (125.229.239.131 臺灣), 05/15/2023 19:06:49 ※ 編輯: celestialgod (125.229.239.131 臺灣), 05/15/2023 19:37:18
文章代碼(AID): #1aOHwdpn (Python)
文章代碼(AID): #1aOHwdpn (Python)