Slå ihop en tabell med sig själv

Joining Data with pandas

Aaren Stubberfield

Instructor

Data om uppföljarfilmer

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

Bild på Toy Story och uppföljaren Toy Story 2

Joining Data with pandas

Slå ihop en tabell med sig själv

Bild på två kopior av samma tabell som slås ihop med sig själv

Joining Data with pandas

Slå ihop en tabell med sig själv

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

Fortsätt formatera resultaten

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

Slå ihop en tabell med sig själv med 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

När ska man slå ihop en tabell med sig själv?

Vanliga situationer:

  • Hierarkiska relationer
  • Sekventiella relationer
  • Grafdata
Joining Data with pandas

Nu kör vi en övning!

Joining Data with pandas

Preparing Video For Download...