Pandas-handling-missing-values

Pandas Join() | Mastering in Python Pandas Library

Pandas Join() Method in Python

import pandas as pd
df1 = pd.DataFrame({'A': [1,2,3],
                   'B': [10,20,30]})


df2 = pd.DataFrame({'C': [4,5,6],
                   'D': [40,50,60]})

display(df1, df2)
Output >>>
             A    B
         0   1   10
         1   2   20
         2   3   30
    
    
             C    D
         0   4   40
         1   5   50
         2   6   60

df1.join(df2)
Output >>>
             A    B   C    D
         0   1   10   4   40
         1   2   20   5   50
         2   3   30   6   60
df2.join(df1)
Output >>>
             C    D   A    B
         0   4   40   1   10
         1   5   50   2   20
         2   6   60   3   30

df1 = pd.DataFrame({'A': [1,2,3],
                   'B': [10,20,30]},
                   index = ['a','b','c'])

df2 = pd.DataFrame({'C': [4,5,6],
                   'D': [40,50,60]},
                  index = ['a','b','c'])

display(df1, df2)
Output >>>          
             A    B
         a   1   10
         b   2   20
         c   3   30
    
    
             C    D
         a   4   40
         b   5   50
         c   6   60

df1.join(df2)
Output >>>
             A    B   C   D
         a   1   10   4  40
         b   2   20   5  50
         c   3   30   6  60

df1 = pd.DataFrame({'A': [1,2,3],
                   'B': [10,20,30]},
                   index = ['a','b','c'])

df2 = pd.DataFrame({'C': [4,5],
                   'D': [40,50]},
                  index = ['a','b'])

display(df1, df2)
Output >>>
              A    B
          a   1   10
          b   2   20
          c   3   30
    
    
              C    D
          a   4   40
          b   5   50
df1.join(df2)
Output >>>
              A    B     C      D
          a   1   10   4.0   40.0
          b   2   20   5.0   50.0
          c   3   30   NaN    NaN
df1.join(df2, how = 'right')
Output >>>          
             A    B   C    D
         a   1   10   4   40
         b   2   20   5   50
df1.join(df2, how = 'inner')
Output >>>
              A    B   C    D
          a   1   10   4   40
          b   2   20   5   50

df1.join(df2, how = 'outer')
Output >>>
              A    B    C       D
          a   1   10   4.0   40.0
          b   2   20   5.0   50.0
          c   3   30   NaN    NaN

df1 = pd.DataFrame({'A': [1,2,3],
                   'B': [10,20,30]})

df2 = pd.DataFrame({'A': [4,5,6],
                   'D': [40,50,60]})

display(df1, df2)
Output >>>
              A    B
          0   1   10
          1   2   20
          2   3   30
    
    
              A   D
          0   4   40
          1   5   50
          2   6   60
df1.join(df2, lsuffix = '_1')
Output >>>
            A_1    B     A      D
          a   1   10   4.0   40.0
          b   2   20   5.0   50.0
          c   3   30   NaN    NaN
df1.join(df2, rsuffix = '_1')
Output >>>                   
              A    B   A_1      D
          a   1   10   4.0   40.0
          b   2   20   5.0   50.0
          c   3   30   NaN    NaN

Leave a Reply