连接多个Pandas数据帧时出现内存错误



我们正在尝试加载IDS-2018数据集,它由10个CSV文件组成,总共6.4 GB。当我们尝试将所有CSV文件连接到32GB RAM服务器中时,它崩溃了(进程被终止(。

我们甚至尝试通过使用来优化熊猫数据帧中的存储空间


def reduce_mem_usage(df):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df[col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df[col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df[col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df[col] = df[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df[col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df[col] = df[col].astype(np.float32)
else:
df[col] = df[col].astype(np.float64)
end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
return df

但是没有用。连接每个CSV文件时,服务器仍然崩溃。我们已经使用pd.concat连接了每个文件。整个代码都在这里。如何做到这一点,以便我们可以做进一步的处理??

我会尝试以下操作:

  • 通过dtypes参数指定read_csv上的列类型
  • 不创建10个数据帧,并且依赖于del
import numpy as np
import pandas as pd
data_files = [
'./data/CSVs/02-14-2018.csv',
'./data/CSVs/02-15-2018.csv',
... # a few more
]
# define dtypes
data_types = {
"col_a": np.float64,
... # other types
}
df = reduce_memory_usage(
pd.read_csv(filename[0], dtype=data_types, index_col=False)
)
for filename[1:] in data_files:
df = pd.concat(
[
df,
reduce_mem_usage(
pd.read_csv(
filename,
dtype=data_types,
index_col=False,
)
),
],
ignore_index=True,
)

通过这种方式,您可以确保类型推断正是您所需要的,并减少内存占用。此外,如果数据中有分类列,这些列通常在CSV文件中编码为字符串,则可以通过使用分类列数据类型来大大减少内存占用。

最新更新