Python practice: Relational Joins & Stacking

Chapter 28 · 8 questions · included in full access

  1. Question 1 · easy

    Primary vs foreign keys in DataFrames

    When joining two DataFrames with pd.merge(), what is the relationship between a primary key in a lookup table and a foreign key in a transactional table?

    Solve it in your browser
  2. Question 2 · easy

    Inner join orders with product catalog

    Load "sales.csv" and "products.csv". Perform an inner join on "Product" using pd.merge(sales, products, on="Product", how="inner"). Print the sum of "Stock" across the merged orders:

    Solve it in your browser
  3. Question 3 · medium

    Left join sales with sales reps master

    Load "sales.csv" and "reps.csv". Perform a left join on ["Rep", "Region"] using pd.merge(sales, reps, on=["Rep", "Region"], how="left"). Print the number of rows in the merged DataFrame:

    Solve it in your browser
  4. Question 4 · medium

    Identify unmatched records with outer join indicator

    Given df_a = pd.DataFrame({"ID": [1, 2, 3], "Val": ["A", "B", "C"]}) and df_b = pd.DataFrame({"ID": [2, 3, 4], "Val": ["X", "Y", "Z"]}), merge on "ID" with how="outer" and indicator=True. Print how many rows appear only in the left table ((merged["_merge"] == "left_only").sum()):

    Solve it in your browser
  5. Question 5 · medium

    Join on differing column names with left_on and right_on

    Load "sales.csv" and "staff.csv". Merge where sales "Rep" matches staff "Name" using pd.merge(sales, staff, left_on="Rep", right_on="Name", how="inner"). Print the average Salary across the matched sales orders:

    Solve it in your browser
  6. Question 6 · easy

    Stack quarterly datasets vertically with pd.concat

    Given q1 = pd.DataFrame({"Month": ["Jan", "Feb"], "Sales": [100, 200]}) and q2 = pd.DataFrame({"Month": ["Mar", "Apr"], "Sales": [300, 400]}), stack them vertically with pd.concat([q1, q2], ignore_index=True). Print the total sum of "Sales":

    Solve it in your browser
  7. Question 7 · medium

    Horizontal concatenation along axis=1

    Given df1 = pd.DataFrame({"A": [1, 2]}) and df2 = pd.DataFrame({"B": [3, 4]}), concatenate side-by-side using pd.concat([df1, df2], axis=1). Print the column names of the resulting DataFrame (list(joined.columns)):

    Solve it in your browser
  8. Question 8 · hard

    Relational margin and target pipeline

    Load "sales.csv" and "reps.csv". Left join on ["Rep", "Region"]. Group by "Rep" and sum "Revenue", merge with "reps.csv" to get "Target", and print the rep who generated the most revenue (top_rep = reps_summary.sort_values("Revenue", ascending=False).iloc[0]["Rep"]):

    Solve it in your browser

These questions are part of full access.

The first chapters of every track are free. Full access is a one-time payment.

See pricing