Merging
How do you merge two DataFrames in Pandas?
You can merge two DataFrames in Pandas using the merge()
function, which allows you to specify the columns to join on and the type of join to perform.
Types of Merges
There are four main types of merges:
- Inner Merge: Returns only matching rows from both DataFrames.
- Left Merge: Returns all rows from the left DataFrame and matching rows from the right DataFrame.
- Right Merge: Returns all rows from the right DataFrame and matching rows from the left DataFrame.
- Outer Merge: Returns all rows from both DataFrames.
import pandas as pd
import numpy as np
# Create a DataFrame 1
df1 = pd.DataFrame({
'Date': ["2024-09-14", "2024-09-15", "2024-09-16"],
'Open': [99,100,101],
'High': [102, 103, 104],
})
# Create DataFrame 2
df2 = pd.DataFrame({
'Date': ["2024-09-14", "2024-09-17", "2024-09-18"],
'Close': [201, 202, 203],
'Low': [198, 199, 200]
})
print("DataFrame 1:")
print(df1)
print("\nDataFrame 2:")
print(df2)
Inner Merge
# Inner merge on 'Date' column
df_inner = pd.merge(df1, df2, on='Date', how='inner')
print("\nInner Merge:")
# Returns only matching rows from both DataFrames
print(df_inner)
Left Merge
# Left merge on 'Date' column
df_left = pd.merge(df1, df2, on='Date', how='left')
print("\nLeft Merge:")
# Returns all rows from the left DataFrame and matching rows from the right DataFrame
print(df_left)
Right Merge
# Right merge on 'Date' column
df_right = pd.merge(df1, df2, on='Date', how='right')
print("\nRight Merge:")
# Returns all rows from the right DataFrame and matching rows from the left DataFrame
print(df_right)