Outer merge: what is missing on each side
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
`books` and `bank` should list the same transactions. Return the references that appear in only one of the two tables, as a DataFrame with the columns `ref` and `source`, where `source` is "books" for a reference that is only in `books` and "bank" for one that is only in `bank`. Row labels and row order do not matter.
What this exercise teaches
An outer merge with an indicator is the standard reconciliation tool: one pass shows what is missing on either side. A left merge would only ever find the references missing from the bank.
A hint if you get stuck
how="outer" keeps the rows of both tables, and indicator=True adds a _merge column that says left_only, right_only or both. Keep the rows that are not both and translate the label.
Try it now, free, no account needed
Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.
Open this exerciseMore exercises in Combining and reshaping
- Inner merge on a shared key
- Left merge keeps every order
- Merge on differently named keys
- Stack two tables with concat
- Merge twice, then aggregate
- Wide to long with melt
- Long to wide with pivot
- Columns into rows with stack
- Index level into columns with unstack
- Orders with no customer, via indicator
- Label the source with concat keys
- One row per tag with explode
- Compare two price lists with suffixes
- Merge on two key columns
- Merge a table with itself