有没有一种方法可以在不占用大量内存的情况下简化 pandas 中大型文件或 Excel 文件的处理?
我现在要做的就是像这样加载文件:
data = pd.read_csv('SUPERLARGEFILE.csv', index_col=0, encoding = "ISO-8859-1", low_memory=False)
Perform some task
data.to_csv('Results.csv', sep=',')
如果我在一台内存较少的计算机上工作。有没有一种方法可以使用迭代函数流式传输和处理大型数据文件来执行以下操作:
Load first 1000 rows, store this in memory
Perform some task
Save data
Load next 1000 rows, over write this in memory
perform task
append to save file
最佳答案
只需将 chunksize 参数添加到您的代码中即可:
data = pd.read_csv('SUPERLARGEFILE.csv', index_col=0, encoding = "ISO-8859-1", low_memory=Fals, chunksize=10)
result = []
for chunk in data: # get chunks of 10 rows each
result.append(chunk.mean())
# do something with res e.g. res = DataFrame(res).to_csv("result.csv")
关于python - 简化 pandas 中大文件的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641484/