Pandas: explore real-world data
Explore tabular data with pandas: load a CSV, inspect its columns, filter and group records, and make a careful plan for missing values.
Learn it here, one small step at a time.
4 guided lessons · About 2h 30m including practice · No subscription
- Load and inspect a DataFrame
- Filter rows and create group summaries
- Handle missing data without hiding its meaning
Recommended first: Python: your first practical programs. These foundations are suggestions, not a requirement to begin.
Load a table and check its shape
Pandas is a Python library for working with tables. A DataFrame has rows and columns. Load a CSV with pd.read_csv("sales.csv"), then inspect df.head(), df.shape, and df.dtypes. These tell you what a few rows look like, how large the table is, and how Python interpreted each column.
First ask what one row represents. A revenue average per transaction means something different from an average per customer. Keep a copy of the source file and check for unexpected headers or dates.
| Command | What it tells you |
|---|---|
| `df.head()` | A few example records |
| `df.shape` | Number of rows and columns |
| `df.dtypes` | How each column is stored |
| `df.isna().sum()` | Missing values in each column |
Load a sample CSV and write down its row count, columns, and what one row means. If a date looks like text, inspect it before converting it.
Reveal a suggested answer
A numeric-looking column may have been loaded as text, or may contain values that need careful conversion.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Select and filter the rows you need
A filter keeps rows that match a condition. For example, df.loc[df["region"] == "North", ["item", "amount"]] selects two columns for North. Combine conditions with & or |, and put each condition in parentheses. Use .loc to make row and column selection clear.
Check how many rows remain after filtering. If the result is empty, check spelling, spaces, casing, and the meaning of your condition.
| Question | Pattern |
|---|---|
| North only | `df.loc[df["region"] == "North"]` |
| Amounts at least 10 | `df.loc[df["amount"] >= 10]` |
| North and amount at least 10 | `(region_is_north) & (amount_is_at_least_10)` |
Filter your table to show North-region sales with amount 10 or more. Print the number of matching rows and compare it with the original.
Reveal a suggested answer
The parentheses make each comparison happen before the `&` or `|`, avoiding ambiguous operator order.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Group rows to answer a question
groupby makes summaries at a chosen level. For example, df.groupby("region")["amount"].sum() calculates a total per region. Use .agg() for several measurements, such as sum and count. Decide the grouping columns before interpreting an average or total.
A grouped table is not the original detail. Keep the detail available so you can trace a surprising summary back to its records.
| Question | Grouping | Calculation |
|---|---|---|
| Total per region | `region` | sum of amount |
| Number of sales per region | `region` | count of sale IDs |
| Average per item and region | `region`, `item` | mean of amount |
Calculate total amount and number of sales per region. Which region has the larger total? Does it also have more transactions?
Reveal a suggested answer
Yes. Fewer transactions may have larger amounts, so total and count answer different questions.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Handle missing values with care
A missing value is not automatically zero. Check where values are missing with df.isna().sum() and compare missingness across important columns. Ask whether the value was unknown, not collected, or not applicable. Document your choice before dropping or filling anything.
For a practice copy only, you could keep incomplete rows visible with df[df["amount"].isna()]. If you decide to exclude rows from one metric, report how many were excluded and why.
| Data value | Possible meaning | Question to ask |
|---|---|---|
| Missing amount | Not recorded | Can the source be checked? |
| Amount = 0 | No sale amount | Is zero a valid measured value? |
| Missing region | Area unknown | Should unknown remain a visible group? |
Find rows with a missing amount. Count them. Decide whether your report can use those rows and describe the limitation rather than silently replacing them with zero.
Reveal a suggested answer
It adds a real zero to the calculation, even though the true amount is unknown; the result can be biased.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Recommended next steps
These paths build on ideas from this course. You can start any of them whenever you are ready.
Your final knowledge check.
Score 80% or more to pass. Sign in to save your best result.
