Sub topic 8
Load, inspect, and plot a dataset
This chapter assumes you have a virtual environment with JupyterLab, pandas, and matplotlib installed, and that the environment is registered as a kernel and selected in your notebook. You should also know basic Python syntax and what a CSV file is.
The working model
A CSV file is plain text. pandas reads it into a DataFrame, a table in memory with labeled columns and an index for rows. Each column is a Series with a single data type (dtype). Once the data is in a DataFrame, you inspect it first and plot it second. Plotting data you have not inspected is how type errors and missing values reach your charts unnoticed.
The workflow has three steps:
- Load the file into a DataFrame.
- Inspect its shape, types, missing values, and summary statistics.
- Plot a column to check its distribution.
Load and inspect
The example assumes a file named data/sales.csv that has a revenue column. The path is relative to the notebook's working directory, which is the folder containing the notebook file. Put each step in its own cell so you can re-run one without the others.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data/sales.csv")
df.head()head() shows the first five rows. Jupyter displays only the value of the last expression in a cell, so head() renders as a table. If you put head(), info(), and describe() in one cell, only describe() shows its table. Use one cell per call, or call print() for the others.
df.info()info() prints the row count, each column's dtype, and the count of non-null values. This is where you find problems:
- A numeric column with dtype
objectusually contains stray text such as"N/A"or a currency symbol. - A non-null count lower than the row count means missing values.
- A date column stored as
objecthas not been parsed as dates.
df.describe()describe() reports count, mean, standard deviation, minimum, quartiles, and maximum for numeric columns. A minimum below zero in revenue, or a maximum far above the 75th percentile, tells you to look at the raw rows before you trust any chart.
Plot a column
df["revenue"].plot(kind="hist", bins=20)
plt.show()A histogram splits the values into bins equal-width ranges and counts how many rows fall in each. It shows the shape of the distribution: skew, gaps, and outliers. Changing bins changes how much detail you see, so try more than one value.
With the inline backend, the figure appears below the cell. Calling plt.show() ends the figure, so the next plot starts on a clean canvas.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `FileNotFoundError` | The working directory differs from what you assumed | Run `import os; os.getcwd()` and adjust the path |
| `ModuleNotFoundError: pandas` | The notebook uses a kernel other than your project environment | Select the registered kernel, then restart it |
| `KeyError: 'revenue'` | Column name differs in case or has stray spaces | Check `df.columns` |
| Histogram fails or looks wrong | The column has dtype `object` | Convert it with `pd.to_numeric(df["revenue"], errors="coerce")` |
Exercise
After running df.info(), you see that revenue has dtype object and 980 non-null values in a 1,000-row file. Name two possible causes and one pandas call that would help you convert the column safely.
Show answer
The column may contain non-numeric text (such as "N/A" or "$1,200") and 20 rows may be empty. Use pd.to_numeric(df["revenue"], errors="coerce") to convert it, then check df["revenue"].isna().sum() to see how many values became missing.