Merging a table to itself

Joining Data with pandas

Aaren Stubberfield

Instructor

Sequel movie data

print(sequel.head())
  id     title        sequel
0 19995  Avatar       NaN
1 862    Toy Story    863
2 863    Toy Story 2  10193
3 597    Titanic      NaN
4 24428  The Avengers NaN

Image of Toy Story and sequel Toy Story 2

Joining Data with pandas

Merging a table to itself

Image of two copies of the same table merged together

Joining Data with pandas

Merging a table to itself

original_sequels = sequels.merge(sequels, left_on='sequel', right_on='id', 
                                 suffixes=('_org','_seq'))
print(original_sequels.head())
  id_org  title_org        sequel_org  id_seq  title_seq        sequel_seq
0 862     Toy Story        863         863     Toy Story 2      10193     
1 863     Toy Story 2      10193       10193   Toy Story 3      NaN       
2 675     Harry Potter...  767         767     Harry Potter...  NaN       
3 121     The Lord of ...  122         122     The Lord of ...  NaN       
4 120     The Lord of ...  121         121     The Lord of ...  122
Joining Data with pandas

Continue format results

print(original_sequels[,['title_org','title_seq']].head())
  title_org        title_seq      
0 Toy Story        Toy Story 2    
1 Toy Story 2      Toy Story 3    
2 Harry Potter...  Harry Potter...
3 The Lord of ...  The Lord of ...
4 The Lord of ...  The Lord of ...
Joining Data with pandas

Merging a table to itself with left join

original_sequels = sequels.merge(sequels, left_on='sequel', right_on='id', 
                                 how='left', suffixes=('_org','_seq'))
print(original_sequels.head())
  id_org  title_org     sequel_org  id_seq  title_seq    sequel_seq
0 19995   Avatar        NaN         NaN     NaN          NaN       
1 862     Toy Story     863         863     Toy Story 2  10193     
2 863     Toy Story 2   10193       10193   Toy Story 3  NaN       
3 597     Titanic       NaN         NaN     NaN          NaN       
4 24428   The Avengers  NaN         NaN     NaN          NaN
Joining Data with pandas

When to merge at table to itself

Common situations:

  • Hierarchical relationships
  • Sequential relationships
  • Graph data
Joining Data with pandas

Let's practice!

Joining Data with pandas

Preparing Video For Download...