In [1]:
# Inner Join
import pandas as pd
df1 = pd.DataFrame({
''''A'''': [''''A0'''', ''''A1'''', ''''A2''''],
''''B'''': [''''B0'''', ''''B1'''', ''''B2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K2'''']
})
df2 = pd.DataFrame({
''''C'''': [''''C0'''', ''''C1'''', ''''C2''''],
''''D'''': [''''D0'''', ''''D1'''', ''''D2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K2'''']
})
result = pd.merge(df1, df2, on=''''key'''')
print(result)
A B key C D 0 A0 B0 K0 C0 D0 1 A1 B1 K1 C1 D1 2 A2 B2 K2 C2 D2
In [2]:
# Left Join
import pandas as pd
df1 = pd.DataFrame({
''''A'''': [''''A0'''', ''''A1'''', ''''A2''''],
''''B'''': [''''B0'''', ''''B1'''', ''''B2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K2'''']
})
df2 = pd.DataFrame({
''''C'''': [''''C0'''', ''''C1'''', ''''C2''''],
''''D'''': [''''D0'''', ''''D1'''', ''''D2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K3'''']
})
result = pd.merge(df1, df2, on=''''key'''', how=''''left'''')
print(result)
A B key C D 0 A0 B0 K0 C0 D0 1 A1 B1 K1 C1 D1 2 A2 B2 K2 NaN NaN
In [3]:
# Right Join
import pandas as pd
df1 = pd.DataFrame({
''''A'''': [''''A0'''', ''''A1'''', ''''A2''''],
''''B'''': [''''B0'''', ''''B1'''', ''''B2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K2'''']
})
df2 = pd.DataFrame({
''''C'''': [''''C0'''', ''''C1'''', ''''C2''''],
''''D'''': [''''D0'''', ''''D1'''', ''''D2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K3'''']
})
result = pd.merge(df1, df2, on=''''key'''', how=''''right'''')
print(result)
A B key C D 0 A0 B0 K0 C0 D0 1 A1 B1 K1 C1 D1 2 NaN NaN K3 C2 D2
In [4]:
# Outer Join
import pandas as pd
df1 = pd.DataFrame({
''''A'''': [''''A0'''', ''''A1'''', ''''A2''''],
''''B'''': [''''B0'''', ''''B1'''', ''''B2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K2'''']
})
df2 = pd.DataFrame({
''''C'''': [''''C0'''', ''''C1'''', ''''C2''''],
''''D'''': [''''D0'''', ''''D1'''', ''''D2''''],
''''key'''': [''''K0'''', ''''K1'''', ''''K3'''']
})
result = pd.merge(df1, df2, on=''''key'''', how=''''outer'''')
print(result)
A B key C D 0 A0 B0 K0 C0 D0 1 A1 B1 K1 C1 D1 2 A2 B2 K2 NaN NaN 3 NaN NaN K3 C2 D2