【发布时间】:2021-07-24 02:11:41
【问题描述】:
我们正在从 SAS 迁移到 Python,但在处理大型数据帧时遇到了一些麻烦。
我正在处理一个有 15kk 行和 44 列的 df,一个相当大的男孩。我需要在某些列中用点替换逗号,删除一些其他列并将一些更改为日期。
要删除,我发现这很好用:
del df['column']
但是当尝试使用这个替换时:
df["column"] = (dfl["column"].replace('\.','', regex=True).replace(',','.', regex=True).astype(float))
我明白了:
MemoryError: Unable to allocate 14.2 MiB for an array with shape (14901054,) and data type uint8
尝试使用此转换为日期时也会发生同样的情况:
df['column'] = pd.to_datetime(df['column'],errors='coerce')
我明白了:
MemoryError: Unable to allocate 114. MiB for an array with shape (14901054,) and data type datetime64[ns]
有没有其他方法可以做这些事情,只是更节省内存?还是事先拆分 df 的唯一解决方案?谢谢!
ps。不是所有的专栏都给我这个问题,但我想这并不重要
【问题讨论】:
标签: python date replace large-data memory-efficient