如何解决大数据帧大于内存
我对Dask还是陌生的,我发现它很有用,但是我有一个我无法解决的问题。
我的数据集大于内存,我想从列中删除重复的值。
问题在于,删除此数据集后,数据集仍将大于内存。因此,需要通过文件计算结果并将其直接保存到磁盘。
当然,我可以构建一个代码来手动执行此删除操作,但是我想知道Dask是否已经实现了此操作。
这是我的代码:
from dask.distributed import Client
import dask.dataframe as dd
client = Client(memory_limit='8GB') # I've tried without this limit
data = dd.read_csv("path_to_file",dtype={
'id': 'Int64'
},sample=1000)
data.drop_duplicates(subset=['text'])
results = data.compute() # <- Here is the problem
results.to_csv("pathout",index=False)
当我调用计算时,结果是一个DataFrame熊猫,在这种情况下,它大于内存。我收到很多:
distributed.nanny - WARNING - Worker exceeded 95% memory budget. Restarting
distributed.nanny - WARNING - Restarting worker
然后执行失败,并显示“ KilledWorker”
编辑:
自包含的示例:
import numpy as np
import pandas as pd
from dask.distributed import Client
import dask.dataframe as dd
# Creates about 2Gb of data
data = np.random.randint(0,10000,size=(2000000,200))
pd.DataFrame(data).to_csv('test_in.csv',index=False)
# If you want to run on terminal,uncomment the next line and identy the rest of the code
# if __name__ == '__main__':
# To test,limit dask to 1Gb
client = Client(n_workers=1,memory_limit='1GB')
df = dd.read_csv('test_in.csv',blocksize='16MB')
results = df.drop_duplicates()
results.to_csv('test_out.csv',index=False)
client.close()
解决方法
from dask.distributed import Client
import dask.dataframe as dd
client = Client(memory_limit='8GB')
data = dd.read_csv("path_to_file",dtype={'id': 'Int64'},sample=1000)
results = data.drop_duplicates(subset=['A']) # Don't call compute here
results.to_csv("pathout",index=False) # Write operations automatically call compute
.compute()将返回Pandas数据帧,Dask从此消失。您可以使用Dask的.to_csv()函数,它将为每个分区保存一个文件。
只需删除.compute()即可,如果每个分区都适合内存,它将起作用。
哦,您需要分配.drop_duplicates()的结果。
,我认为你的工人被杀了,因为 drop_duplicates 将 df.npartitions 重置为 1。 尝试在前后打印 df.npartitions 以确保。
你可以尝试的一件事是 results = df.drop_duplicates(split_out=df.npartitions) 这仍然需要很长时间来计算艰难..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。