Inner join two dataframes

Inner join is a type of merge operation in Pandas that combines two DataFrames based on common values in a specific column. It only returns the rows where there is a match in both DataFrames. It’s useful when we need to combine data from different sources that have some shared data points or to perform analysis on data that has common features.

Inorder to join two dataframes by 2 columns so that they have only the common rows in Pandas, we can use the merge function. Here are three methods:

1. Using "merge" with inner join:

  • This method uses the merge function with the inner join type to keep only the rows that have matching values in both dataframes.
  • The `on` parameter specifies the columns to join on.
Example:

2. Using "intersection of indexes":

  • This method first sets the `indexes` of the two dataframes to the columns we want to join on and then gets their intersection.
  • It then uses the `merge function` with `inner join` type and the common index to keep only the rows that have matching values in both dataframes.
Example:

3. Using "indexes":

  • This method also sets the `indexes` of the two dataframes to the columns we want to join on.
  • Then uses the `join` function with inner join type and the common index to keep only the rows that have matching values in both dataframes.
Example: