# How do I find the common values in two different dataframe by comparing different column names?

If the data frames are as follows :
df1
column names = UniqueID  Age Gender
values = 213,21,F
145,34,M
890,23,F
df2
column names = ID Occupation Bill
145 Teacher 45RS
890 Student 70RS
456 Teacher 90Rs


I want to use a function which finds the common values in "UniqueID" from df1 and "ID" from df2 and gets stored in df3. Also, the only columns which exist in df3 should be of "common ids" and the columns of df2.

You can use pandas.merge() to get the common rows based on the columns. Try this:

df3 = pd.merge(df1, df2, how='inner', left_on='UniqueID', right_on='ID')


However, this will give you all the columns. After that you can use

df3.drop([column_names], inplace=True)


to drop the columns that are not required.

• Thank you, this works!
– Sca
Jun 17 '19 at 5:07
• You may mark the answer as accepted to close the question if your problem is solved. :) Jun 17 '19 at 5:12
• Yes I did mark the answer as accepted.
– Sca
Jun 18 '19 at 4:15
df3 = df1[df['series_name'].isin(df2['series_name'])]


EDIT:

Adding details per request. This simple approach can compare across 2 different dataframes, placing the output in a third.

• Can you please add some details around your solution as in, the answer should include some information about the approach and why it was chosen over others. Jun 21 at 15:43