Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyspark: merge (outer-join) two data frames

Tags:

I have the following two data frames:

DF1:

    Id | field_A | field_B | field_C | field_D
     1 |   cat   |  12     |   black | 11
     2 |   dog   | 128     |   white | 19
     3 |   dog   |  35     |  yellow | 20
     4 |   dog   |  21     |   brown |  4
     5 |  bird   |  10     |    blue |  7
     6 |   cow   |  99     |   brown | 34

and

DF2:

    Id | field_B | field_C | field_D | field_E
     3 |  35     |  yellow | 20      |   123   
     5 |  10     |    blue |  7      |   454   
     6 |  99     |   brown | 34      |   398   

And I am hoping to get the new_DF as

    Id | field_A | field_B | field_C | field_D | field_E
     1 |   cat   |  12     |   black | 11      |
     2 |   dog   | 128     |   white | 19      |
     3 |   dog   |  35     |  yellow | 20      |  123
     4 |   dog   |  21     |   brown |  4      |  
     5 |  bird   |  10     |    blue |  7      |  454
     6 |   cow   |  99     |   brown | 34      |  398

Could this be achieved by data frame operations? Thanks!

like image 761
Edamame Avatar asked Jun 27 '16 21:06

Edamame


1 Answers

try this:

new_df = df1.join(df2, on=['field_B', 'field_C', 'field_D'], how='left_outer')
like image 66
MaxU - stop WAR against UA Avatar answered Sep 22 '22 09:09

MaxU - stop WAR against UA