Python for data analysis.
Write it. Check it.
Build a small analysis, verify the result and explain your decisions. Learn Python and pandas through practical exercises with reproducible inputs.
Choose a practical learning path
Use Python to turn a business question into a repeatable analysis. Start with the language basics, then practise loading, checking and transforming small tables. Move on when you can explain what each step does and verify its output.
01 · Language basics
Practise lists, dictionaries, conditions, loops and functions. Write a function that handles empty input and explain how it behaves.
02 · Work with tables
Use pandas to inspect types, filter rows, group records and join tables. State what one row represents before changing its shape.
03 · Validate the result
Check duplicate keys, missing values and totals. Treat a plausible-looking output as something to verify, not proof of correctness.
04 · Explain the analysis
Choose a chart or table that answers the question. Describe the population, time period and limits of the conclusion.
Work through a small order analysis
This self-contained example uses four invented orders. Each row is one order; the amount is an integer number of US dollars (USD). The question is: which customers have the largest total value of completed orders?
Run this in a local Python environment or notebook with pandas installed. The code is shown as a learning example; this page does not execute Python.
import pandas as pd
orders = pd.DataFrame({
"order_id": [101, 102, 103, 104],
"customer_id": [1, 2, 1, 3],
"amount": [500, 300, 200, 900],
"status": ["completed", "completed", "returned", "completed"]
})
assert orders["order_id"].is_unique
assert orders["amount"].notna().all()
assert orders["customer_id"].notna().all()
completed = orders.loc[orders["status"].eq("completed")]
summary = (
completed.groupby("customer_id", as_index=False)
.agg(order_count=("order_id", "size"), revenue=("amount", "sum"))
.sort_values(["revenue", "customer_id"], ascending=[False, True])
)
assert summary["revenue"].sum() == completed["amount"].sum()
print(summary.to_string(index=False))| customer_id | order_count | revenue |
|---|---|---|
| 3 | 1 | 900 |
| 1 | 1 | 500 |
| 2 | 1 | 300 |
The completed-order total is US$1,700. The US$200 returned order is excluded under this exercise’s rule. In a production analysis, confirm whether revenue should account for partial refunds, taxes and shipping rather than copying that rule automatically.
The second sort key makes equal-revenue rows deterministic. Grouping changes the table from one row per order to one row per customer. State that change when explaining your code.
Check before and after a merge
Before joining orders to a customer table, confirm that the customer lookup has one row per customer. Duplicate lookup keys can repeat order rows and inflate revenue.
customers = pd.DataFrame({
"customer_id": [1, 2, 3],
"city": ["Mumbai", "Delhi", "Pune"]
})
joined = completed.merge(
customers, on="customer_id", how="left",
validate="many_to_one", indicator=True
)
assert len(joined) == len(completed)
assert joined["amount"].sum() == completed["amount"].sum()
unmatched = joined.loc[joined["_merge"].eq("left_only")]
print(len(unmatched)) # Expected: 0The validation option checks the expected relationship; it does not check that every order has a matching customer. The indicator column helps you inspect unmatched rows. Decide how to handle those rows based on the analysis, instead of dropping them silently.
Unlike a typical SQL equality join, pandas can match null keys to one another. Inspect missing keys explicitly. See the pandas merge documentation for the behaviour and supported options.
Use the library that fits the task
pandas
For labelled tables, grouping, joins and reshaping. Practise explaining the row count and grain of each intermediate result.
Read the guide →NumPy
For arrays and numerical calculations. Learn array shapes and how broadcasting affects an operation.
Matplotlib and Seaborn
For charts. Start with a readable bar or line chart, label the units and make the comparison period explicit.
Python standard library
Use datetime for time values and collections for useful containers. A small, clear function is often enough for a simple transformation.
Interview requirements vary by role. Use the job description to decide which areas need more depth; no single library list applies to every interview.
Practise the explanation as well as the code
- Add a second completed order for customer 1. Predict the new ranking before running the code.
- Duplicate a customer lookup row. Confirm that the validated merge fails, then investigate the duplicate instead of suppressing the error.
- Remove customer 3 from the lookup. Explain why a left join preserves the order and how you would report the missing city.
- Add a missing amount. Decide whether it represents unknown value, zero value or invalid data before selecting a cleaning rule.
- Show the result as a bar chart. Explain why customer totals in this tiny dataset do not establish a general spending pattern.
A useful interview answer
Describe the input grain, the filtering rule, the transformation and the checks. For example: “I kept completed orders, grouped by customer, and reconciled the grouped total to the filtered input. I would confirm the refund definition before using this as a revenue report.”
Continue learning
Business cases
Use the output to answer a defined question and explain what it cannot prove.
Read the guide →For guided feedback, review services and session details. The learning content above is available without an account.