【发布时间】:2017-02-11 00:07:43
【问题描述】:
我有两个熊猫数据帧df1 和df2,格式相当标准:
one two three feature
A 1 2 3 feature1
B 4 5 6 feature2
C 7 8 9 feature3
D 10 11 12 feature4
E 13 14 15 feature5
F 16 17 18 feature6
...
df2 的格式相同。这些 DataFrame 的大小约为 175MB 和 140 MB。
merged_df = pd.merge(df1, df2, on='feature', how='outer', suffixes=('','_features'))
我得到以下内存错误:
File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/tools/merge.py", line 39, in merge
return op.get_result()
File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/tools/merge.py", line 217, in get_result
join_index, left_indexer, right_indexer = self._get_join_info()
File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/tools/merge.py", line 353, in _get_join_info
sort=self.sort, how=self.how)
File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/tools/merge.py", line 559, in _get_join_indexers
return join_func(lkey, rkey, count, **kwargs)
File "pandas/src/join.pyx", line 187, in pandas.algos.full_outer_join (pandas/algos.c:61680)
File "pandas/src/join.pyx", line 196, in pandas.algos._get_result_indexer (pandas/algos.c:61978)
MemoryError
合并时是否有可能对 pandas 数据框有“大小限制”?我很惊讶这行不通。也许这是某个版本的熊猫的一个错误?
编辑:如 cmets 中所述,合并列中的许多重复项很容易导致 RAM 问题。见:Python Pandas Merge Causing Memory Overflow
现在的问题是,我们如何进行这种合并?似乎最好的方法是以某种方式对数据框进行分区。
【问题讨论】:
-
您的
feature列中有重复项吗?如果有很多重复项,您的联接最终可能会非常很大 -
@maxymoo 是的。你能解释一下为什么这会超过 RAM 限制吗?假设
df1有1000万行,feature1有500K行,feature2有500K行等等。数据帧本身只有150 MB---为什么会出现内存错误? -
你用的是32位python/64位吗?
-
如果 feature1 在 df1 中有 500K 行,在 df2 中有 500K 行,那么连接将有 500K*500K=250B 行
-
@SayaliSonawane 我现在明白了。不,这里没有 NaN 值 :)
标签: python pandas memory dataframe out-of-memory