Oct 5, 2022

[Python] join two dataframe according to specific column

 To join two dataframe (df1, df2) according to specific column (e.g., X,Y,Z),


# merge two dataframe comparing X column data of df1 and Y column data of df2

# merge rows where X value on df1 is same to Y value of df2  (other rows will be abandoned)

# in the merged dataframe, "_x" will be added to column label of df1 and "_y" will be added to column label of df2

          pd.merge(df1, df2, left_on="X", right_on="Y")     


# comparing multiple columns

# rows with same X, Y, Z values on df1 and df2 will merge  (other rows will be abandoned)

# in the merged dataframe, "_x" will be added to column label of df1 and "_y" will be added to column label of df2 

          pd.merge(df1, df2, left_on=['X ','Y','Z'], right_on=['X ','Y','Z'])


No comments:

Post a Comment