在 pandas 中处理大量大型数据文件
Handling a large amount of large data files in pandas
我需要评估来自 DEM 模拟的 1800 个数据文件。每个数据文件在特定时间点有效,并包含一个粒子列表及其温度。我想绘制一部分粒子的平均温度随时间的变化。
不幸的是,在评估期间一段时间后,我 运行 内存不足。每个数据文件大约有 15 MB。这是我所做的:
import pandas as pd
import numpy as np
import linecache
import os as os
import gc
path = "E:/Simulationen/35_1100_700/DEM/post/dump/"
timesList = [] # create empty list for time
TcentralList = [] # create empty list for temperatures
for files in os.walk(os.path.normpath(path)):
for file in files[2]: # files is a tuple with a list of filenames in the third element (index 2) of the tuple
time = (int(file[0:6])-300000)*0.1+3 # read the timestamps from filenames (first six characters) and convert to time
timesList.append(time) # write time to times list for later creation of dataframe
# Read the headerline (line 9), write items to column title list
coltitles = [sub.replace('[0]','') for sub in linecache.getline(path+file,9).split()[2:]]
columns=list(range(0,len(coltitles),1)) # list of columns to read
df = pd.read_csv(path+file, sep=' ', skiprows=8, index_col=0, usecols=columns)
df.columns = coltitles[1:]
df.index.names = [coltitles[0]]
T_central = df[df.r.le(0.01) & df.z.ge(0.045) & df.z.lt(0.055)]['f_Temp'].mean(axis=0) # Filter all rows (particles) where radius r is lower/equal than 0.01 m and z is between 0.045 m (greater/equal) and 0.055 m (lower) and average their temperatures
# List of average temperatures of central particles for later creation of dataframe
TcentralList.append(T_central)
我正在读取路径中的所有文件。时间是从文件名中获取的,转换并存储在列表中 - 我稍后想创建一个带有 'time' 和 'temperature' 列的数据框。然后我将数据文件读取到数据框并仅过滤中心区域的粒子,然后平均它们的温度。
数据文件有 17 列。我尝试的第一件事是通过缩短列表 'columns' 来只读取必要的列,但这并没有减少内存使用。
然后我尝试手动启动垃圾收集(gc):
gc.collect()
del df
del T_central
这也没有帮助。我还尝试重新初始化 df 和 T_central 以删除对它们的引用
T_central=[]
df=pd.DataFrame()
但没有任何效果。
我没主意了。有人给我提示吗?
干杯,
塞巴斯蒂安
无意中找到解决办法:不是pandas而是linecache.getline()
占用内存太多。手动指定要读取的列和数据框的列标题解决了它。
我需要评估来自 DEM 模拟的 1800 个数据文件。每个数据文件在特定时间点有效,并包含一个粒子列表及其温度。我想绘制一部分粒子的平均温度随时间的变化。 不幸的是,在评估期间一段时间后,我 运行 内存不足。每个数据文件大约有 15 MB。这是我所做的:
import pandas as pd
import numpy as np
import linecache
import os as os
import gc
path = "E:/Simulationen/35_1100_700/DEM/post/dump/"
timesList = [] # create empty list for time
TcentralList = [] # create empty list for temperatures
for files in os.walk(os.path.normpath(path)):
for file in files[2]: # files is a tuple with a list of filenames in the third element (index 2) of the tuple
time = (int(file[0:6])-300000)*0.1+3 # read the timestamps from filenames (first six characters) and convert to time
timesList.append(time) # write time to times list for later creation of dataframe
# Read the headerline (line 9), write items to column title list
coltitles = [sub.replace('[0]','') for sub in linecache.getline(path+file,9).split()[2:]]
columns=list(range(0,len(coltitles),1)) # list of columns to read
df = pd.read_csv(path+file, sep=' ', skiprows=8, index_col=0, usecols=columns)
df.columns = coltitles[1:]
df.index.names = [coltitles[0]]
T_central = df[df.r.le(0.01) & df.z.ge(0.045) & df.z.lt(0.055)]['f_Temp'].mean(axis=0) # Filter all rows (particles) where radius r is lower/equal than 0.01 m and z is between 0.045 m (greater/equal) and 0.055 m (lower) and average their temperatures
# List of average temperatures of central particles for later creation of dataframe
TcentralList.append(T_central)
我正在读取路径中的所有文件。时间是从文件名中获取的,转换并存储在列表中 - 我稍后想创建一个带有 'time' 和 'temperature' 列的数据框。然后我将数据文件读取到数据框并仅过滤中心区域的粒子,然后平均它们的温度。 数据文件有 17 列。我尝试的第一件事是通过缩短列表 'columns' 来只读取必要的列,但这并没有减少内存使用。 然后我尝试手动启动垃圾收集(gc):
gc.collect()
del df
del T_central
这也没有帮助。我还尝试重新初始化 df 和 T_central 以删除对它们的引用
T_central=[]
df=pd.DataFrame()
但没有任何效果。
我没主意了。有人给我提示吗?
干杯, 塞巴斯蒂安
无意中找到解决办法:不是pandas而是linecache.getline()
占用内存太多。手动指定要读取的列和数据框的列标题解决了它。