簡體   English   中英

基於第三個數據幀在 Pandas 中加入兩個數據幀

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

我有 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

第三個數據幀具有df1col3df2 col6的組合。 看起來像

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

有沒有其他有效的方法來做到這一點?

以上工作正常,我這邊沒有內存錯誤。 我正在運行帶有 32 位 python 的 8Gig Ram 計算機。

  • 為您的計算機提供更多空間
  • 檢查並停止內存(Ram)使用率高的應用程序(主要是chrome標簽)
  • 如果上述數據框不是您使用的,則限制數據行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM