join dataframes code example

Example 1: join on column pandas

# df1 as main df and use the feild from df2 and map it into df1

df1.merge(df2,on='columnName',how='left')

Example 2: pandas left join

df.merge(df2, left_on = "doc_id", right_on = "doc_num", how = "left")

Example 3: joins in pandas

pd.merge(product,customer,how='inner',left_on=['Product_ID','Seller_City'],right_on=['Product_ID','City'])

Example 4: join tables pandas

In [99]: result = left.join(right, on=['key1', 'key2'], how='inner')

Example 5: Joins with another DataFrame

# Joins with another DataFrame

df.join(df2, df.name == df2.name, 'outer').select(
  df.name, df2.height).collect()
# [Row(name=None, height=80), Row(name=u'Bob', height=85), Row(
#   name=u'Alice', height=None)]

df.join(df2, 'name', 'outer').select('name', 'height').collect()
# [Row(name=u'Tom', height=80), Row(name=u'Bob', height=85), Row(
#   name=u'Alice', height=None)]

cond = [df.name == df3.name, df.age == df3.age]
df.join(df3, cond, 'outer').select(df.name, df3.age).collect()
# [Row(name=u'Alice', age=2), Row(name=u'Bob', age=5)]

df.join(df2, 'name').select(df.name, df2.height).collect()
# Row(name=u'Bob', height=85)]

df.join(df4, ['name', 'age']).select(df.name, df.age).collect()
# [Row(name=u'Bob', age=5)]

Example 6: join in pandas

import pandas as pd

clients = {'Client_ID': [111,222,333,444,555],
           'Client_Name': ['Jon Snow','Maria Green', 'Bill Jones','Rick Lee','Pamela Lopez']
           }
df1 = pd.DataFrame(clients, columns= ['Client_ID','Client_Name'])


countries = {'Client_ID': [111,222,333,444,777],
             'Client_Country': ['UK','Canada','Spain','China','Brazil']
             }
df2 = pd.DataFrame(countries, columns= ['Client_ID', 'Client_Country'])


Inner_Join = pd.merge(df1, df2, how='inner', on=['Client_ID', 'Client_ID'])
print(Inner_Join)