我希望能够通过Python在给定两个或更多参数的大型数据集中检索特定行(9M行,1.4gb)。在
例如,从这个数据集中:ID1 2 10 2 2 1 2 2 2 2 2 1
ID2 10 12 2 2 2 2 2 2 2 1 2
ID3 2 22 0 1 0 0 0 0 0 1 2
ID4 14 45 0 0 0 0 1 0 0 1 1
ID5 2 8 1 1 1 1 1 1 1 1 2
给定示例参数:第二列必须等于2,并且
第三列必须在
范围从4到15
我应该得到:
^{pr2}$
问题是我不知道如何在Python中的二维数组上高效地执行这些操作。在
这就是我所尝试的:line_list = []
# Loading of the whole file in memory
for line in file:
line_list.append(line)
# set conditions
i = 2
start_range = 4
end_range = 15
# Iteration through the loaded list and split for each column
for index in data_list:
data = index.strip().split()
# now test if the current line matches with conditions
if(data[1] == i and data[2] >= start_range and data[2] <= end_range):
print str(data)
我想执行这个过程很多次,我这样做的方式是非常缓慢的,即使数据文件加载在内存中。在
我在考虑使用numpy数组,但我不知道如何在给定条件下检索行。在
谢谢你的帮助!在
更新:
正如建议的那样,我使用了关系数据库系统。
我选择Sqlite3是因为它非常容易使用,而且部署也很快。在
我的文件是通过sqlite3中的导入函数在大约4分钟内加载的。在
我在第二列和第三列上做了索引,以加快检索信息的过程。在
查询是通过Python完成的,模块为“sqlite3”。在
就这样,快得多!在