I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience
3+ years at Mu Sigma
2 years at OYO
1 year and counting at The Data Monk
I am an active trader and a logically sarcastic idiot :)
Follow Me
Joins are basically of 4 Types: Inner Join, Outer Join, Left Join, Right Join.
Lets understand each one of them.
Inner Join: Suppose we perform inner join on two tables df1 and df2. The resultant table will contain values that are common in both the tables.
Left Join: Suppose we perform left join on two tables df1 and df2. The resultant table will contain all the rows from the left table along with the common rows of both the tables.
Right Join : Suppose we perform right join on two tables df1 and df2. The resultant table will contain all the rows from the right table along with the common rows of both the tables.
Outer Join:In outer joins, every row from the left and right dataframes is retained in the result, with NaNs where there are no matched join variables.
In the above code,
t1 and t2 are 2 tables
in on parametre, you pass on the column name which you want to use to perform join.
how parametre indicates the type of join you want to perform like left, right,outer,inner.
Answers ( 4 )
pd.merge(df1,df2,on=”col”,how=”inner”)
col=”COlumn name on which you would merge”
how=”inner”/”outer”/”left”/”right”
In python, joining to two tables is done by merge command from pandas library as
import pandas as pd
#dataframe 1
df1 = pd.DataFrame({‘col1’: [0, 1], ‘col_left’: [‘a’, ‘b’]})
#dataframe 2
df2 = pd.DataFrame({‘col1’: [1, 2, 2], ‘col_right’: [2, 2, 2]})
#Joining
pd.merge(df1, df2, on=’col1′, how=’outer’, indicator=True)
Here, on=keys and how=type of join
Joins are basically of 4 Types: Inner Join, Outer Join, Left Join, Right Join.
Lets understand each one of them.
Inner Join: Suppose we perform inner join on two tables df1 and df2. The resultant table will contain values that are common in both the tables.
Left Join: Suppose we perform left join on two tables df1 and df2. The resultant table will contain all the rows from the left table along with the common rows of both the tables.
Right Join : Suppose we perform right join on two tables df1 and df2. The resultant table will contain all the rows from the right table along with the common rows of both the tables.
Outer Join:In outer joins, every row from the left and right dataframes is retained in the result, with NaNs where there are no matched join variables.
import pandas pd
pd.merge(t1,t2,on=’col_name’,how=”left”)
In the above code,
t1 and t2 are 2 tables
in on parametre, you pass on the column name which you want to use to perform join.
how parametre indicates the type of join you want to perform like left, right,outer,inner.