'add new column based on mutual value in 2 dataframes

I have 2 dataframes:

df1:

enter image description here

and df2:

enter image description here

They have differnet size. The common parts between these dataframes is the columns B (df1) and Z (df2), and based on columns B and Z I want to obtain new dataframe:

enter image description here

That add the major of student that are common between df1 and df2 and if it doesn't exist in df2 have empty value.



Solution 1:[1]

You can merge the two dataframes on the B and Z columns, e.g.:

df = pd.merge(left=df1, right=df2, left_on='B', right_on='Z', how='left')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1