【问题标题】:Join two Data Frames in Pandas based on third data frame根据第三个数据框加入 Pandas 中的两个数据框
【发布时间】:2020-03-17 10:23:17
【问题描述】:

我有 3 个 不同尺寸的熊猫 df。 第一帧如下-

df1.head(4)

col1   col2   col3
  a      b     c
  d      e     f
  g      h     i
  j      k     l

第二帧如下-

df2.head(4)

col4   col5   col6
  m      n      o
  p      q      r
  s      t      u
  v      w      x

第三个数据框具有col3df1col6df2 的组合。好像

df3.head(3)

col3    col6
  c       r
  i       u
  f       x

现在我想根据df3col3col6 中的组合组合所有三个数据框。结果 df 应该看起来像-

final_df.head(3)

col1    col2    col4    col5    col2    col6
  a      b       p        q       c       r
  g      h       s        t       i       u
  d      e       v        w       f       x

我试过下面的代码

df4 = pd.merge(df1, df3, on='col3')
final_df = pd.merge(df4, df2, on='col6')

但出现内存错误

MemoryError: Unable to allocate 1.79 GiB for an array with shape (2, 120193432) and data type int64

还有其他有效的方法吗?

【问题讨论】:

    标签: python pandas dataframe join merge


    【解决方案1】:

    以上工作正常,我这边没有内存错误。我正在使用 32 位 python 运行 8Gig Ram 计算机。

    • 为您的计算机提供更多空间
    • 检查并停止使用高内存 (Ram) 的应用程序(主要是 chrome 选项卡)
    • 如果上述数据框不是您使用的,则限制数据行

    【讨论】:

    • 我正在使用 i5 8 gen、Mint 64 位进行 8 场演出。尽管如此,这还是行不通。我在一个数据框中有超过 50k 行。
    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多