简体   繁体   English

基于第三个数据帧在 Pandas 中加入两个数据帧

[英]Join two Data Frames in Pandas based on third data frame

I have 3 pandas df with different dimensions .我有 3 个不同尺寸的熊猫 df 。 The first frame is as follows-第一帧如下——

df1.head(4)

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

The second frame is as follows-第二帧如下——

df2.head(4)

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

The third data frame has combinations of col3 of df1 and col6 of df2 .第三个数据帧具有df1col3df2 col6的组合。 It looks like看起来像

df3.head(3)

col3    col6
  c       r
  i       u
  f       x

Now I want to combine all three data frames based on the combinations in df3 columns col3 and col6 .现在我想根据df3col3col6的组合组合所有三个数据框。 The resultant df should look like-结果 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

I have tried the following code我试过下面的代码

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

but got the memory error as但得到了内存错误

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

Is there any other effecient way to do this ?有没有其他有效的方法来做到这一点?

The above works fine without a memory error in my side.以上工作正常,我这边没有内存错误。 I am running an 8Gig Ram Computer with 32bit python.我正在运行带有 32 位 python 的 8Gig Ram 计算机。

  • Give More space to your Computer为您的计算机提供更多空间
  • Check and stop apps with high Memory(Ram) usage (mostly chrome tabs)检查并停止内存(Ram)使用率高的应用程序(主要是chrome标签)
  • Limit data rows if the above dataframes is not what you working with如果上述数据框不是您使用的,则限制数据行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM