pandas 搜寻字段

楼主: gbllggi (gbllggi)   2016-06-28 01:36:20
大家好
我有一个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加起来,再转换成资料?
谢谢
作者: ccwang002 (亮)   2016-06-28 03:35:00
为何不用 database (ex SQLite)?
作者: CaptainH (Cannon)   2016-06-28 11:40:00
output那行看起来像O(N^2)操作把+改+=试试?

Links booklink

Contact Us: admin [ a t ] ucptt.com