Joining Data in Pandas

Combine multiple DataFrames

In Pandas, you can join (combine) multiple DataFrames using functions like:

  • join() → Joining on index
  • concat() → Stacking DataFrames vertically or horizontally
  • merge() → SQL-like joins (inner, left, right, outer)
import pandas as pd

# Employees Data
df1 = pd.DataFrame({
    'Employee_ID': [101, 102, 103, 104],
    'Name': ['Jasmeet', 'Bob', 'Charlie', 'David'],
    'Dept_ID': [1, 2, 1, 3]
})

# Departments Data
df2 = pd.DataFrame({
    'Dept_ID': [1, 2, 3, 4],
    'Dept_Name': ['HR', 'IT', 'Finance', 'Marketing']
})

print(df1)
print(df2)

Using join() for Index-based Joins

  • df.join() is used when joining sDataFrames based on their index instead of a column.
df1.set_index("Employee_ID", inplace=True)
df2.set_index("Dept_ID", inplace=True)

df_joined = df1.join(df2, how="left")
print(df_joined)

Output

             Name  Dept_ID Dept_Name
Employee_ID                         
101        Jasmeet       1        HR
102          Bob       2        IT
103      Charlie       1        HR
104        David       3   Finance

Using concat() for Stacking DataFrames

concat() is used to combine DataFrames vertically (axis=0) or horizontally (axis=1).

Appending Rows (axis=0)

df3 = pd.DataFrame({
    "Employee_ID": [105, 106],
    "Name": ["Eve", "Frank"],
    "Dept_ID": [2, 3]
})

df_concat = pd.concat([df1, df3], ignore_index=True)
print(df_concat)

Output

   Employee_ID   Name       Dept_ID
0         101    Jasmeet    1
1         102    Bob        2
2         103    Charlie    1
3         104    David      3
4         105    Eve        2
5         106    Frank      3

New employees Eve and Frank are added.

Using concat() for Side-by-Side Merge

  • If DataFrames share the same index, you can merge them horizontally.
df4 = pd.DataFrame({
    "Employee_ID": [101, 102, 103, 104],
    "Salary": [50000, 60000, 70000, 55000]
})

df_concat_horiz = pd.concat([df1.set_index("Employee_ID"), df4.set_index("Employee_ID")], axis=1)
print(df_concat_horiz)

Output

              Name          Dept_ID  Salary
Employee_ID                        
101           Jasmeet       1        50000
102           Bob           2        60000
103           Charlie       1        70000
104           David         3        55000

DataFrames are merged side by side.

Summary Table

MethodDescription
join()Joins based on index
concat([df1, df2], axis=0)Stacks DataFrames vertically
concat([df1, df2], axis=1)Merges DataFrames side by side
No questions available.