作者gbllggi (gbllggi)
看板Python
標題pandas 搜尋欄位
時間Tue Jun 28 01:36:20 2016
大家好
我有一個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
→ ccwang002: 為何不用 database (ex SQLite)? 06/28 03:35
推 CaptainH: output那行看起來像O(N^2)操作 06/28 11:40
→ CaptainH: 把+改+=試試? 06/28 11:41