Python practice: Relational Joins & Stacking
Chapter 28 · 8 questions · included in full access
Question 1 · easy
Primary vs foreign keys in DataFrames
Solve it in your browserWhen 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?Question 2 · easy
Inner join orders with product catalog
Solve it in your browserLoad
"sales.csv"and"products.csv". Perform an inner join on"Product"usingpd.merge(sales, products, on="Product", how="inner"). Print the sum of"Stock"across the merged orders:Question 3 · medium
Left join sales with sales reps master
Solve it in your browserLoad
"sales.csv"and"reps.csv". Perform a left join on["Rep", "Region"]usingpd.merge(sales, reps, on=["Rep", "Region"], how="left"). Print the number of rows in the merged DataFrame:Question 4 · medium
Identify unmatched records with outer join indicator
Solve it in your browserGiven
df_a = pd.DataFrame({"ID": [1, 2, 3], "Val": ["A", "B", "C"]})anddf_b = pd.DataFrame({"ID": [2, 3, 4], "Val": ["X", "Y", "Z"]}), merge on"ID"withhow="outer"andindicator=True. Print how many rows appear only in the left table ((merged["_merge"] == "left_only").sum()):Question 5 · medium
Join on differing column names with left_on and right_on
Solve it in your browserLoad
"sales.csv"and"staff.csv". Merge where sales"Rep"matches staff"Name"usingpd.merge(sales, staff, left_on="Rep", right_on="Name", how="inner"). Print the averageSalaryacross the matched sales orders:Question 6 · easy
Stack quarterly datasets vertically with pd.concat
Solve it in your browserGiven
q1 = pd.DataFrame({"Month": ["Jan", "Feb"], "Sales": [100, 200]})andq2 = pd.DataFrame({"Month": ["Mar", "Apr"], "Sales": [300, 400]}), stack them vertically withpd.concat([q1, q2], ignore_index=True). Print the total sum of"Sales":Question 7 · medium
Horizontal concatenation along axis=1
Solve it in your browserGiven
df1 = pd.DataFrame({"A": [1, 2]})anddf2 = pd.DataFrame({"B": [3, 4]}), concatenate side-by-side usingpd.concat([df1, df2], axis=1). Print the column names of the resulting DataFrame (list(joined.columns)):Question 8 · hard
Relational margin and target pipeline
Solve it in your browserLoad
"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"]):
These questions are part of full access.
The first chapters of every track are free. Full access is a one-time payment.
See pricing