Data Analysis with Python (NumPy, Pandas, Data Wrangling)
Data Analysis with Python (NumPy, Pandas, Data Wrangling)
This domain tests loading, inspecting, cleaning, transforming, and summarizing tabular data with pandas, plus NumPy vectorization. Questions often hinge on label versus position indexing, missing-value handling, and merge behavior.
Core rules
- A Series is labeled one-dimensional data; a DataFrame is two-dimensional with an index and columns.
.locis label-based and inclusive;.ilocis position-based and stop-exclusive.- Prefer vectorized operations and
groupbyoveriterrows. - Diagnose missingness before imputing;
isna,dropna, andfillnaare different choices, not equivalents. groupbyfollows split-apply-combine;aggcan return multiple statistics.- Merges use
how='inner','left','right', or'outer'; validate keys and row counts after merging. - Convert types deliberately with
astypeandpd.to_datetime.
Worked example
Compute revenue by region after filling missing prices with the median price for each product:
df['price'] = df['price'].fillna(df.groupby('product')['price'].transform('median'))
df['revenue'] = df['units'] * df['price']
out = df.groupby('region')['revenue'].agg(['sum', 'mean', 'count'])
Reasoning: transform('median') returns a Series aligned to the original rows, so assignment fills only missing values. A product-level median is more defensible than a global median when prices differ by product. The final agg produces one row per region with three statistics.
Traps
- Chained assignment causing SettingWithCopyWarning or silent non-updates.
- Assuming
inplace=Truealways returns the modified object. - Merging on non-unique keys and inflating row counts.
- Treating NaN as zero before checking why it is missing.
- Forgetting that
.ilocexcludes the stop position.