Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python bcolz如何合并两个ctables_Python_Pandas - Fatal编程技术网

Python bcolz如何合并两个ctables

Python bcolz如何合并两个ctables,python,pandas,Python,Pandas,我在这里玩bcolz内存压缩示例 到目前为止,我真的对这个图书馆感到惊讶。我认为这是一个很好的工具,对于我们所有想把大文件加载到小内存中的人来说(如果你正在读这篇文章的话,干得好,Francesc!) 我想知道是否有人有加入两个ctables(如pandas.merge())的经验,以及如何有效利用时间/内存 谢谢分享您的想法:-) 我及时拿到了。。非常感谢@mdurant为itertoolz提供的服务!!下面是一些伪代码,因为我使用的示例非常难看 # here's generic pandas

我在这里玩bcolz内存压缩示例

到目前为止,我真的对这个图书馆感到惊讶。我认为这是一个很好的工具,对于我们所有想把大文件加载到小内存中的人来说(如果你正在读这篇文章的话,干得好,Francesc!)

我想知道是否有人有加入两个ctables(如pandas.merge())的经验,以及如何有效利用时间/内存


谢谢分享您的想法:-)

我及时拿到了。。非常感谢@mdurant为itertoolz提供的服务!!下面是一些伪代码,因为我使用的示例非常难看

# here's generic pandas
df_new = pd.merge(df1,df2) 


# example with itertoolz and bcolz
from toolz.itertoolz import join as joinz
import bcolz

#convert them to ctables
zdf1 = bcolz.ctable.fromdataframe(df1)
zdf2 = bcolz.ctable.fromdataframe(df2)

#column 2 of df1 and column 1 of df2 were the columns to join on
merged = list(joinz(1,zdf1.iter(),0,zdf2.iter()))

# where new_dtypes are the dtypes of the fields you are using
# mine new_dtypes= '|S8,|S8,|S8,|S8,|S8'
zdf3 = bcolz.fromiter(((a[0]+a[1]) for a in merged), dtype = new_dtypes, count = len(merged))
显然,可能有一些更聪明的方法,这个例子并不是很具体,但它是有效的,可以作为一个基础,让人们进一步构建它

以美国东部时间10月21日晚上7点为例进行编辑
如您所见,我创建的版本比pandas慢15倍,但是仅使用迭代器将节省大量内存。请随意对此发表评论和/或展开讨论。bcolz似乎是一个很好的构建包。

bcolz附带了迭代器,因此在执行无法简单表示为求值的操作时,需要迭代两个输入。您是想要完整的合并行为还是更具体的行为?@mdurant,谢谢您的回答:-)。最好是具有与pd.merge相同的功能范围。但是我已经喜欢使用迭代器/生成器的想法了。我将对此进行研究,并尝试将其与简单的MapReduce结合起来。如果我有了显著的改进,我将在这里发布这篇文章,这篇文章在迭代器上工作得很慢,可能是一条出路——然后输出(也是一个interator)应该通过管道传回bcolz@主持人:这是一个很好的暗示。下一次当我有一个好的usecaseFYI toolz.join时,我会在线尝试它。join只是在正确的参数中进行流式处理。我可能会从ctable中提取numpy块作为重新排列,将它们合并到内存中,然后将它们附加到目标表中。
#download movielens data files from http://grouplens.org/datasets/movielens/
#I'm using the 1M dataset
import pandas as pd
import time
from toolz.itertoolz import join as joinz
import bcolz

t0 = time()
dset = '/Path/To/Your/Data/'
udata = os.path.join(dset, 'users.dat') 
u_cols = ['user_id', 'age', 'sex', 'occupation', 'zip_code']
users = pd.read_csv(udata,sep='::',names=u_cols)

rdata = os.path.join(dset, 'ratings.dat')
r_cols = ['user_id', 'movie_id', 'rating', 'unix_timestamp']
ratings = pd.read_csv(rdata, sep='::', names=r_cols)

print ("Time for parsing the data: %.2f" % (time()-t0,)) 
#Time for parsing the data: 4.72

t0=time()
users_ratings = pd.merge(users,ratings)
print ("Time for merging the data: %.2f" % (time()-t0,))
#Time for merging the data: 0.14

t0=time()
zratings = bcolz.ctable.fromdataframe(ratings)
zusers = bcolz.ctable.fromdataframe(users)
print ("Time for ctable conversion: %.2f" % (time()-t0,))
#Time for ctable conversion: 0.05

new_dtypes = ','.join([x[0].str for x in zusers.dtype.fields.values()][::-1] +[y[0].str for y in zratings.dtype.fields.values()][::-1])

#Do the merge with a list stored intermediately
t0 = time()
merged = list(joinz(0,zusers.iter(),0,zratings.iter()))
zuser_zrating1 = bcolz.fromiter(((a[0]+a[1]) for a in merged), dtype = new_dtypes, count = len(merged))
print ("Time for intermediate list bcolz merge: %.2f" % (time()-t0,))
#Time for intermediate list bcolz merge: 3.16

# Do the merge ONLY using iterators to limit memory consumption
t0 = time()
zuser_zrating2 = bcolz.fromiter(((a[0]+a[1]) for a in joinz(0,zusers.iter(),0,zratings.iter())) , dtype = new_dtypes, count = sum(1 for _ in joinz(0,zusers.iter(),0,zratings.iter())))
print ("Time for 2x iters of merged bcolz: %.2f" % (time()-t0,))
#Time for 2x iters of merged bcolz: 3.31