How to apply inner join on two DataFrames?

Hello everyone, I am seeking your help in writing correct code to apply an inner join on two DataFrames. I am currently working on a project that requires me to merge two DataFrames based on a common column. However, I am relatively new to coding and struggling with the syntax for applying an inner join in Python.
Here is the code:

import pandas as pd

# create two sample dataframes
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df2 = pd.DataFrame({'A': [2, 3, 4], 'B': [5, 6, 7], 'D': [8, 9, 10]})

# join the two dataframes on columns A and C with an inner join
result = pd.merge(df1, df2, on=['A', 'C'], how='inner')

# display the joined dataframe

KeyError: 'C'

I would appreciate any guidance or examples of correct code for applying an inner join on two DataFrames in Python.

1 Like

Hey @nimrah, it is giving keyError that means at the time of merging it is unable to find the same column in both data frames. You can remove this error by using the below solution:

It seems that your code is raising a KeyError which indicates that the columns on which you’re trying to join the two dataframes are not common. A possible solution to this problem is to set the indexes of both dataframes to the common columns you want to join on. Here’s an example code that demonstrates this approach:

In this code, we use the join() function with an inner join type, which means that only the rows with common indexes are included in the output.