Pandas & Data Analysis·Pandas from first Series to analysis project

Merging tables

Join DataFrames with merge, choose the correct how and validate cardinality.

Beginner 14 min read Lesson 7 of 300 Challenge included
What you will be able to do
  • Use on= for matching columns.
  • Use how to control which rows survive.
  • Use validate="one_to_many" to catch bad data.
  • Check row counts before and after.

In plain English

merge is a SQL join for DataFrames. Choose how="inner", "left", "right" or "outer" based on which rows must survive. Validate the relationship with validate= so a surprise many-to-many join fails loudly.

merge.pyPython
import pandas as pd

courses = pd.DataFrame({'slug': ['html', 'css'], 'title': ['HTML', 'CSS']})
lessons = pd.DataFrame({'course_slug': ['html', 'css', 'css'], 'minutes': [12, 18, 22]})

merged = lessons.merge(courses, left_on='course_slug', right_on='slug', how='left', validate='many_to_one')
print(merged.groupby('title')['minutes'].sum())
Your turn

Left merge

Medium

Merge lessons with courses on course_slug/slug keeping every lesson.

PYTHON · starter
lessons.merge(courses, left_on='course_slug', right_on='slug')
Check your understanding before moving on.
Knowledge checkQuestion 1

Which how value keeps every row from the left DataFrame?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Dates and times
Lesson 7 of 300 · 0% of course