Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to Merge or Join DataFrame in Python Using Pandas

Merging DataFrames

Condition for Merging or Joining DataFrames

  • Description:
    In Pandas, merging or joining DataFrames is a process of combining two or more datasets based on a common column or index.

    Merge: Combines DataFrames using one or more common columns or indices.

    Join: Combines DataFrames on their indices or on a specified column.
Step-by-Step Process
  • Import Pandas Library:
    Import the necessary Pandas library.
  • Create DataFrames:
    Create sample data using pd.DataFrame().
  • Merge or Join DataFrames:
    Use pd.merge() or df.join() to combine DataFrames.
  • Specify Merge Keys:
    Define the columns or indices to merge on.
Sample Source Code
  • # Code for Merging DataFrames

    import pandas as pd

    df1 = pd.DataFrame({
    'ID': [1, 2, 3, 4],
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [25, 30, 35, 40]
    })

    df2 = pd.DataFrame({
    'ID': [1, 2, 4, 5],
    'Salary': [50000, 60000, 70000, 80000],
    'Department': ['HR', 'IT', 'Finance', 'Marketing']
    })

    print("DataFrame 1:")
    print(df1)

    print("\nDataFrame 2:")
    print(df2)

    # Merge DataFrames using 'ID' as the common column (Inner Join by default)
    merged_inner = pd.merge(df1, df2, on='ID', how='inner')

    print("\nInner Merge (Common 'ID' values):")
    print(merged_inner)

    # Left Join DataFrames using 'ID'
    merged_left = pd.merge(df1, df2, on='ID', how='left')

    print("\nLeft Merge (All rows from df1):")
    print(merged_left)

    # Right Join DataFrames using 'ID'
    merged_right = pd.merge(df1, df2, on='ID', how='right')

    print("\nRight Merge (All rows from df2):")
    print(merged_right)

    # Outer Join DataFrames using 'ID'
    merged_outer = pd.merge(df1, df2, on='ID', how='outer')

    print("\nOuter Merge (All rows from both df1 and df2):")
    print(merged_outer)
Screenshots
  • Merge and Join Output