Skip to content
tezvyn:

How would you combine customer and transaction DataFrames and describe join types?

Source: pandas.pydata.orgMediumHow cards are made

How would you combine customer and transaction DataFrames and describe join types?

This tests relational merging and join semantics in pandas. Answer: use pd.merge on customer_id, then groupby sum; describe inner, left, right, and outer joins by key preservation. Red flag: proposing concat without keys or conflating inner and left joins.

What's really being asked

This question probes your ability to model a one-to-many relationship using pandas merge operations and your fluency with SQL-style join semantics. The interviewer wants to see that you recognize the cardinality between customers and transactions, know the appropriate API, and can explain the tradeoffs between join types rather than simply reciting syntax.

The full answer

First, state that you would use pd.merge to combine the DataFrames on the customer_id column, because merge is the idiomatic tool for SQL-style joins on keys. Second, note that after merging you would group by customer_id and sum the amount column to get totals, which respects the one-to-many structure. Third, describe the four common join types in order of practical usefulness: inner join keeps only customers who have at least one transaction; left join keeps every customer and shows NaN for missing transactions; right join keeps every transaction row and drops customers without transactions; outer join preserves the full union of both tables. Fourth, mention that for this specific business question a left join is usually safest because you want to retain customers with zero spend and fill missing totals with zero, unless the requirement explicitly excludes inactive customers. You can also note that DataFrame.join could work if customer_id were set as the index, but pd.merge is the standard approach for column-based keys.

The mistakes people make

A red flag is suggesting pd.concat or DataFrame.join without referencing the customer_id key, because concat stacks rows or columns and does not perform key-based relational joins. Another red flag is describing join types incorrectly, such as saying inner and left joins are the same or claiming a right join keeps all customers. Candidates also stumble by forgetting to group after the merge, instead trying to sum before joining, which breaks the one-to-many aggregation. Some also propose iterating rows with apply, which destroys vectorized performance.

What usually comes next

The interviewer may ask how you would handle customers with no transactions, which tests whether you chose left join and handled NaN values with fillna. They might ask about performance on large datasets, where you should mention ensuring the key is sorted or using PyArrow-backed strings. They could also ask about duplicate customer_ids in the customer table, which would turn the merge into a many-to-many relationship and inflate transaction totals. Another follow-up is asking when outer joins become memory-intensive, which happens when both tables contain large numbers of unmatched keys.

A concrete example

Imagine customers has ids 1, 2, 3 and transactions has ids 1 and 3. An inner merge returns customers 1 and 3 with their totals. A left merge returns all three customers, with customer 2 showing a NaN total that you would replace with zero after grouping. A right merge would return only 1 and 3 but preserve any orphan transactions with missing customer data. An outer merge returns the full set of 1, 2, and 3 plus any orphan transactions.

Interview question

You need to combine customers and transactions on customer_id to compute total spend per customer, including those with zero transactions. Which approach correctly uses pandas?

  • a.Use pd.concat to align the tables on customer_id, then group by customer and sum amounts
  • b.Use pd.merge with how='left' on customer_id, then group by customer, sum amounts, and fill NaN totals with zeroCorrect
  • c.Use pd.merge with how='inner' on customer_id, then group by customer and sum amounts
  • d.Use pd.merge with how='right' on customer_id, then group by customer and sum amounts
Why?

A left join preserves every customer and shows NaN for missing transactions, which you fill with zero after grouping; an inner join is tempting because it is the pandas default, but it silently drops customers with no matching transactions.

Just read this? Test yourself on what you have been reading.

Read the original → pandas.pydata.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles