Exercise 2: Why "UNKNOWN" Produces NaN Rather Than Being Dropped or Erroring — Possible Solution ==================================================================== WHAT how="left" GUARANTEES, PER THIS CHAPTER ------------------------------ Per this chapter's own compare-table, a "left" join "keeps... every row from the left table, matched where possible." In pd.merge(df, stores_df, on="store_id", how="left"), df (the sales table) is the left table — so every single row that exists in df is guaranteed to appear in the result, regardless of whether a matching store_id exists in stores_df. WHY "UNKNOWN" HAS NO MATCH IN stores_df ------------------------------ Per this chapter's own stores_df example table, only two store_id values exist there: "S1" and "S2." The value "UNKNOWN" — the placeholder ds1-4 filled row 1003's missing store_id with — was never a real store identifier and was never going to appear in a lookup table of actual stores. There is, correctly, no row in stores_df matching "UNKNOWN." WHY THIS PRODUCES NaN RATHER THAN DROPPING THE ROW ------------------------------ Because how="left" guarantees every row from df survives regardless of whether a match exists, the merge cannot simply drop the "UNKNOWN" row for lacking a match — that would violate the very guarantee "left" makes. Instead, for that one row, the columns being pulled in from stores_df (store_name and city) have nothing to be filled with, so pandas fills them with NaN — the same missing-value marker ds1-4 already introduced for representing "no value available here." The row itself remains fully present with its original sales data intact; only the two newly-joined columns are empty for that specific row. WHY THIS DOESN'T CAUSE AN ERROR ------------------------------ A missing match on a left join is an entirely expected, routine outcome of the join operation itself, not a malformed or invalid input — the whole reason how="left" exists as a distinct option from how="inner" is specifically to handle this situation gracefully (keep the row, leave the unmatched columns empty) rather than treating it as exceptional. Only an "inner" join would have excluded this row instead, per this chapter's own compare-table describing "inner" as keeping "only rows with a match in both tables." WHY THIS WORKS AS AN ANSWER ------------------------------ It explains precisely what how="left" guarantees (every left-table row survives), why "UNKNOWN" specifically has no counterpart in stores_df, and why the combination of those two facts produces NaN in the newly-joined columns rather than either dropping the row or raising an error — tying the outcome directly back to this chapter's own compare-table definition of a left join.