python - Pandas 合并错误 : MemoryError

标签 python merge pandas

问题:

我正在尝试将两个相对较小的数据集放在一起,但合并引发了一个MemoryError。我有两个国家贸易数据聚合数据集,我试图在关键年份和国家/地区合并,因此数据需要特殊放置。不幸的是,这使得 concat 的使用及其性能优势无法实现,如以下问题的答案所示:MemoryError on large merges with pandas in Python .

这是设置:

尝试合并:

df = merge(df, i, left_on=['year', 'ComTrade_CC'], right_on=["Year","Partner Code"])

基本数据结构:

我:

    Year    Reporter_Code   Trade_Flow_Code Partner_Code    Classification  Commodity Code  Quantity Unit Code  Supplementary Quantity  Netweight (kg)  Value   Estimation Code
0    2003    381     2   36  H2  070951  8   1274    1274    13810   0
1    2003    381     2   36  H2  070930  8   17150   17150   30626   0
2    2003    381     2   36  H2  0709    8   20493   20493   635840  0
3    2003    381     1   36  H2  0507    8   5200    5200    27619   0
4    2003    381     1   36  H2  050400  8   56439   56439   683104  0

df:

    mporter  cod     CC ComTrade_CC Distance_miles
0    110     215     215     757     428.989
1    110     215     215     757     428.989
2    110     215     215     757     428.989
3    110     215     215     757     428.989
4    110     215     215     757     428.989

错误回溯:

 MemoryError                      Traceback (most recent call last)
<ipython-input-10-8d6e9fb45de6> in <module>()
      1 for i in c_list:
----> 2     df = merge(df, i, left_on=['year', 'ComTrade_CC'], right_on=["Year","Partner Code"])

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0rc1_309_g9fc8636-py2.7-linux-x86_64.egg/pandas/tools/merge.pyc in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy)
     36                          right_index=right_index, sort=sort, suffixes=suffixes,
     37                          copy=copy)
---> 38     return op.get_result()
     39 if __debug__:
     40     merge.__doc__ = _merge_doc % '\nleft : DataFrame'

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0rc1_309_g9fc8636-py2.7-linux-x86_64.egg/pandas/tools/merge.pyc in get_result(self)
    193                                       copy=self.copy)
    194 
--> 195         result_data = join_op.get_result()
    196         result = DataFrame(result_data)
    197 

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0rc1_309_g9fc8636-py2.7-linux-x86_64.egg/pandas/tools/merge.pyc in get_result(self)
    693                 if klass in mapping:
    694                     klass_blocks.extend((unit, b) for b in mapping[klass])
--> 695             res_blk = self._get_merged_block(klass_blocks)
    696 
    697             # if we have a unique result index, need to clear the _ref_locs

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0rc1_309_g9fc8636-py2.7-linux-x86_64.egg/pandas/tools/merge.pyc in _get_merged_block(self, to_merge)
    706     def _get_merged_block(self, to_merge):
    707         if len(to_merge) > 1:
--> 708             return self._merge_blocks(to_merge)
    709         else:
    710             unit, block = to_merge[0]

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0rc1_309_g9fc8636-py2.7-linux-x86_64.egg/pandas/tools/merge.pyc in _merge_blocks(self, merge_chunks)
    728         # Should use Fortran order??
    729         block_dtype = _get_block_dtype([x[1] for x in merge_chunks])
--> 730         out = np.empty(out_shape, dtype=block_dtype)
    731 
    732         sofar = 0

MemoryError: 

谢谢你的想法!

最佳答案

万一遇到这个问题的人仍然遇到与 merge 类似的问题,您可以通过将两个数据框中的相关列重命名为相同的名称来使 concat 工作名称,将它们设置为 MultiIndex(即 df = dv.set_index(['A','B'])),然后使用 concat加入他们。

更新

例子:

df1 = pd.DataFrame({'A':[1, 2], 'B':[2, 3], 'C':[3, 4]})
df2 = pd.DataFrame({'A':[1, 2], 'B':[2, 3], 'D':[7, 8]})
both = pd.concat([df1.set_index(['A','B']), df2.set_index(['A','B'])], axis=1).reset_index()

df1

    A   B   C
0   1   2   3
1   2   3   4

df2

    A   B   D
0   1   2   7
1   2   3   8

两者

    A   B   C   D
0   1   2   3   7
1   2   3   4   8

我没有对这种方法的性能进行基准测试,但它没有出现内存错误并且适用于我的应用程序。

关于python - Pandas 合并错误 : MemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085280/

相关文章:

merge - 将 2 个文件合并到第三个文件中,使用列作为索引并合并行

Python 根据另一列中的 Nan 或 boolean 值在一列中移动值

python-3.x - Pandas_datareader错误SymbolWarning:未能读取符号:“T”,替换为NaN

python - 合并 Pandas 数据框列表

python - 转置 pandas 数据框并垂直 append

python - openCV中的旋转矩阵

git - Git 最好的可视化 merge 工具是什么?

python - 扩展给定列表的字母数字范围

python - 属性错误 : lower not found; using a Pipeline with a CountVectorizer in scikit-learn

jpa - @OneToOne 需要 CascadeType.MERGE 吗?