gpt4 book ai didi

Python bcolz 如何合并两个 ctables

转载 作者:太空狗 更新时间:2023-10-29 18:08:12 24 4
gpt4 key购买 nike

我正在玩这个 notebook 中的内存压缩示例中的 bcolz

到目前为止,我对这个库感到非常惊讶。我认为它对于我们所有人来说都是一个很好的工具,可以将更大的文件加载到较小的内存中(Francesc 干得好,如果您正在阅读这篇文章!)

我想知道是否有人有像使用 pandas.merge() 那样加入两个 ctables 的经验,以及如何做到这一点/内存有效。

感谢分享您的想法:-)!

最佳答案

我及时得到它..非常感谢@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 点

#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

如您所见,我创建的版本比 pandas 慢 15 倍,但是通过仅使用迭代器,它将节省大量内存。请随意发表评论和/或对此进行扩展。 bcolz 似乎是一个很好的构建包。

关于Python bcolz 如何合并两个 ctables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25741898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com