Data Exploration and Analysis with Pandas and NumPy
What this domain tests
This domain covers exploratory analysis in pandas and NumPy: selecting and filtering, grouping and aggregating, reshaping, and vectorized computation. The exam checks whether you can move from a question to a correct, reproducible summary.
Core rules
locis label-based andilocis position-based; uselocfor boolean masks and column names.- Boolean masks combine with
&,|, and~and need parentheses:df[(df.a > 0) & (df.b == 'x')]. groupby(...).agg(...)produces one row per key;transformreturns an aligned column;pivot_tablereshapes long to wide.- NumPy operations are vectorized:
arr * 2,np.where(cond, a, b), and broadcasting across shapes.axis=0reduces rows,axis=1reduces columns. - Always check
.shape,.dtypes, and.head()after each transformation.
Worked example
You have sales with region, revenue, and units. To find each region's mean revenue and its share of total revenue, run g = sales.groupby('region', as_index=False).agg(mean_rev=('revenue', 'mean'), n=('revenue', 'size')), then g['share'] = g['mean_rev'] * g['n'] / sales['revenue'].sum(). Reasoning: the weighted share must use total revenue per region, which is mean times count, not the mean alone; as_index=False keeps region as a column for later merges.
Traps that cost marks
- Confusing
axis=0andaxis=1in NumPy reductions. - Using
andororinstead of&or|on Series, which raises an ambiguity error. - Expecting
groupbyto keep NaN keys by default;dropna=Truehides them. - Forgetting that NumPy integer arrays overflow silently at fixed widths.
- Chaining many operations without intermediate checks, so a dtype change goes unnoticed.