Pandas DataFrames - Inner Join
Concat Pandas DataFrames with Inner Join
You can inner join two DataFrames during concatenation which results in the intersection of the two DataFrames.
Syntax
The syntax of concat() function to inner join is given below.
pd.concat([df1, df2], axis=1, join='inner')
Inner join results in a DataFrame that has intersection along the given axis to the concatenate function.
Examples
1. Inner join DataFrames along an axis
In this example, we shall take two DataFrames and find their inner join along axis=1.
Python Program
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
df2 = pd.DataFrame({'B': ['B2', 'B3', 'B4', 'B5'],
'D': ['D2', 'D3', 'D4', 'D5'],
'F': ['F2', 'F3', 'F4', 'F5']},
index=[2, 3, 4, 5])
result = pd.concat([df1, df2], axis=1, join='inner')
print(result)
Output
D:\>python example.py
A B C D B D F
2 A2 B2 C2 D2 B2 D2 F2
3 A3 B3 C3 D3 B3 D3 F3
2. Inner join DataFrames along axis=0
In this example, we shall try the inner join of DataFrames along a different axis from that of the previous example.
Python Program
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
df2 = pd.DataFrame({'B': ['B2', 'B3', 'B4', 'B5'],
'D': ['D2', 'D3', 'D4', 'D5'],
'F': ['F2', 'F3', 'F4', 'F5']},
index=[2, 3, 4, 5])
result = pd.concat([df1, df2], axis=0, join='inner')
print(result)
Output
D:\>python example.py
B D
0 B0 D0
1 B1 D1
2 B2 D2
3 B3 D3
2 B2 D2
3 B3 D3
4 B4 D4
5 B5 D5
3. Inner join more than two DataFrames using concat()
In this example, we shall take more than two DataFrames, which is three, and find the inner join of these DataFrames along an axis.
Python Program
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
df2 = pd.DataFrame({'B': ['B2', 'B3', 'B4', 'B5'],
'D': ['D2', 'D3', 'D4', 'D5'],
'F': ['F2', 'F3', 'F4', 'F5']},
index=[2, 3, 4, 5])
df3 = pd.DataFrame({'D': ['D3', 'D4', 'D5'],
'G': ['G3', 'G4', 'G5']},
index=[3, 4, 5])
result = pd.concat([df1, df2, df3], axis=1, join='inner')
print(result)
Output
D:\>python example.py
A B C D B D F D G
3 A3 B3 C3 D3 B3 D3 F3 D3 G3
Summary
In this tutorial of Python Examples, we learned how to inner join two or more DataFrames using concatenate function along an axis.