pandas 搜尋欄位

看板Python作者 (gbllggi)時間7年前 (2016/06/28 01:36), 7年前編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
大家好 我有一個pandas DataFrame 九欄三萬多列,想做一個搜尋的function 可以搜尋每一欄,然後產出一個df包含所有“包含”關鍵字的資料 我現在的方法就是每一欄分開搜尋,再加在一起 str_low = search_keyword.lower() a = (df.loc[df['column_1'].str.lower().str.contains(str_low) == True]) b = (df.loc[df['column_2'].str.lower().str.contains(str_low) == True]) c = (df.loc[df['column_3'].str.lower().str.contains(str_low) == True]) d = (df.loc[df['column_4'].str.lower().str.contains(str_low) == True]) e = (df.loc[df['column_5'].str.lower().str.contains(str_low) == True]) f = (df.loc[df['column_6'].str.lower().str.contains(str_low) == True]) g = (df.loc[df['column_7'].str.lower().str.contains(str_low) == True]) h = (df.loc[df['column_8'].str.lower().str.contains(str_low) == True]) i = (df.loc[df['column_9'].str.lower().str.contains(str_low) == True]) output = a + b + c + d + e + f + g + h + i 不僅看起來笨,速度也超慢 大概是要把每一個欄位先變成小寫再比較這邊拖慢了速度吧 但除此之外,還有比較有效率的辦法可以搜尋過所以欄位嗎? 例如直接搜過所有的欄?或是先把boolean array加起來,再轉換成資料? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 108.58.165.58 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1467048984.A.685.html ※ 編輯: gbllggi (108.58.165.58), 06/28/2016 01:37:17 ※ 編輯: gbllggi (108.58.165.58), 06/28/2016 01:41:06

06/28 03:35, , 1F
為何不用 database (ex SQLite)?
06/28 03:35, 1F

06/28 11:40, , 2F
output那行看起來像O(N^2)操作
06/28 11:40, 2F

06/28 11:41, , 3F
把+改+=試試?
06/28 11:41, 3F
文章代碼(AID): #1NSMGOQ5 (Python)