Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · PANDAS · CHAPTER 10

Chapter 10 worked answers: Operations, GroupBy and Exploratory Analysis

On this page

Compare your reasoning after an attempt. Different valid implementations are welcome when they preserve the observation set and the stated meaning.

B2-10-E01 · Count support correctly

Report row count and observed-value count for each group.

size counts rows; count excludes missing values in the selected field.

df=pd.DataFrame({"group":["A","A","B"],"value":[2,None,5]})
summary=df.groupby("group").agg(rows=("value","size"),observed=("value","count"))
assert summary.loc["A"].tolist()==[2,1]
print(summary)
       rows  observed
group                
A         2         1
B         1         1

B2-10-E02 · Compare weights

Calculate the mean subgroup proportion and pooled proportion; explain their different weighting.

Equal subgroup weighting and equal underlying-registration weighting answer different questions.

success=np.array([9,30])
total=np.array([10,60])
mean_rate=(success/total).mean();pooled=success.sum()/total.sum()
assert mean_rate==.7 and np.isclose(pooled,39/70)
print(mean_rate,pooled)
0.7 0.5571428571428572

B2-10-E03 · Check a subgroup reversal

Verify that A has the higher rate in both tasks while B has the higher pooled rate.

Different task composition reverses the pooled comparison. This construction does not itself establish a causal advantage.

a_success=np.array([9,20]);a_total=np.array([10,100])
b_success=np.array([80,1]);b_total=np.array([100,10])
assert ((a_success/a_total)>(b_success/b_total)).all()
assert a_success.sum()/a_total.sum() < b_success.sum()/b_total.sum()
print(a_success/a_total,b_success/b_total)
[0.9 0.2] [0.8 0.1]

Return to practice

Change one supplied input, predict the effect and rerun your own implementation. Return to the exercise prompts to identify a task you can now explain independently.

Teaching edition · verified experiments · PDF and EPUB editions not generated.