Flipkart Interview Question | How to join tables in python?

Question

.

in progress 0
TheDataMonk 4 years 4 Answers 1089 views Grand Master 0

Answers ( 4 )

  1. pd.merge(df1,df2,on=”col”,how=”inner”)

    col=”COlumn name on which you would merge”
    how=”inner”/”outer”/”left”/”right”

  2. 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

  3. 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.

  4. 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.

Leave an answer

Browse
Browse