merge()

noun

say it: “MURJ” /ˈmɝdʒ/

A pandas method that joins two tables side by side, matching their rows by a column they share, such as a student ID.

What does merge() do in pandas?

merge() is the pandas method that joins two tables side by side by a key column they share, the job a spreadsheet's VLOOKUP does. on= names the key, and how= says which rows without a match are kept ("inner", "left", "right" or "outer").

import pandas as pd
scores = pd.DataFrame({"student_id": [101, 102], "score": [88, 92]})
students = pd.DataFrame({"student_id": [101, 102], "name": ["Ana", "Ben"]})
print(scores.merge(students, on="student_id"))
#    student_id  score name
# 0         101     88  Ana
# 1         102     92  Ben

In a sentence

  • “merge() added each student's name to their quiz scores by student ID.”
  • “With how='left', merge() kept the students who belong to no club.”

Level: Journeyman · More Python terms at this level

Updated