作者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://webptt.com/cn.aspx?n=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
1F:→ ccwang002: 为何不用 database (ex SQLite)? 06/28 03:35
2F:推 CaptainH: output那行看起来像O(N^2)操作 06/28 11:40
3F:→ CaptainH: 把+改+=试试? 06/28 11:41