Flipkart Interview Question | How to join tables in python?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
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.