Python · easy

Merge two DataFrames on a key and keep unmatched rows from the left.

Asked in Data Analyst interviews, in the Python round.

Short answer

pd.merge(left, right, on='key', how='left').

How to answer it

how="left" keeps every row of the left frame; right-side columns are NaN where there was no match.

out = left.merge(right, on="user_id", how="left", validate="many_to_one", indicator=True)
out["_merge"].value_counts()      # both / left_only: how many rows found a match

Two arguments carry the interview. validate="many_to_one" asserts that user_id is unique on the right; without it, a duplicated key on the right fans out the left rows and the row count grows silently. indicator=True adds a column saying which side each row came from, which is how you report "12% of orders had no matching user" instead of noticing it a week later.

If the key has different names, left_on and right_on; if the types differ (int on one side, string on the other), nothing matches and pandas does not warn. Check out["_merge"].eq("both").mean() after any merge you care about.

Related questions

Practice this for real