我有csv格式下的100-300Go数据(数字+ unicode文本),需要在此基础上进行常规的数据透视表作业。谷歌搜索/ StackOverflowing后,找不到满意的答案(只有部分)。 想知道哪种解决方案对于单机(64Go RAM)最快:
1)转换并插入PostGres并通过SQL在PostGres DB中处理所有内容? (或MySQL ......)
2)在Pandas中的块中加载csv并手动逐个处理?
3)加载csv +转换为HDF并按块处理HDF?
4)其他解决方案。
答案 0 :(得分:0)
对于在单台机器上工作,PostgreSQL可能是您列出的3个选项中最合适的。
为了控制内存使用情况,您可以使用partitioning并按块处理数据。
答案 1 :(得分:0)
发问者必须已经解决了问题,对于可能会提出该问题的其他人,我的回答可能会有所帮助。 尝试此解决方案(根据您的数据集进行转换),我尝试在50-80 GB的范围内添加numpy可以提高性能。
import pandas as pd
from datetime import date
from datetime import datetime
print("1 Load rec =", datetime.now())
df6 = pd.read_csv('sampleDataframeFlux/sampleDataframeFlux.csv',low_memory=False, memory_map=True,engine='c',na_filter=False,index_col=False,usecols=["time", "Label","Server","value"])
print("shape",df6.shape)
print("2 Create dataframe =",datetime.now())
df6["Label"]=df6["Server"]+"|"+df6["Label"]
df6.drop(['Server'],axis=1,inplace=True)
print("3 Time trim =", datetime.now())
df6['time']=df6['time']//1000000000
print("shape",df6.shape)
print("4 Round Epoch to nearest multiple of 5 =", datetime.now())
df6['time']=5*round(df6['time']/5)
print("shape",df6.shape)
print("5 Pivot dataframe=", datetime.now())
df6=df6.pivot_table(index='time', columns=["Label"],values="value",fill_value=0)
print("shape",df6.shape)
print("6 Epoch to UTC =", datetime.now())
df6.index=pd.to_datetime(df6.index, unit='s')
print("7 Convert to type category to reduce memory =", datetime.now())
df6=df6.astype('category')
print("shape",df6.shape)
print("8 Start to write to a file =", datetime.now())
df6.to_csv('file_11.csv', header=True, chunksize=500000)
print("9 Finish =", datetime.now())