Week 2 — Lesson 1: pandas in ten commands

2 min

NorthPeak Manufacturing sends one reading per machine per day, and the file data/clean/readings.csv holds 14,600 rows for 40 machines. Before any model, you need to open that file, count it, and ask it simple questions. pandas is the Python library that does this: it puts the CSV into a table called a DataFrame. Ten commands do most of the work, and this lesson names them.

The DataFrame and the ten commands

pandas is the Python library for tables. A table is a DataFrame. It has rows and named columns. One column alone is a Series. You read a CSV with pd.read_csv. Then you ask questions with short commands.

Here are the ten commands you will use all year. Learn them by name.

CommandWhat it does
pd.read_csv(path)Loads a CSV file into a DataFrame
df.shapeNumber of rows and columns, as (rows, columns)
df.head()The first five rows
df["col"]One column, as a Series
df[["a", "b"]]Several columns, as a smaller DataFrame
df[df["col"] > 80]The rows where a condition is true
df["col"].mean()The mean of a column; also .min(), .max(), .sum()
df.sort_values("col")Rows sorted by a column; add ascending=False for biggest first
df.groupby("key")["col"].mean()One mean per group
df["col"].nunique()How many different values a column has

On the NorthPeak readings

Every command below was run on data/clean/readings.csv. The comment shows the result.

python
import pandas as pd

readings = pd.read_csv("data/clean/readings.csv")
print(readings.shape)                                    # (14600, 10)
print(round(readings["temperature_c"].mean(), 1))        # 46.2
print(readings[readings["temperature_c"] > 80].shape)    # (133, 10)
print(readings["machine_id"].nunique())                  # 40
print(readings.groupby("machine_id")["temperature_c"].mean().round(1).head(3))

The last line prints one mean per machine. M001 is at 47.2 degrees, M002 at 47.9, M003 at 53.3. The .head(3) keeps three rows. Without it you would see forty.

The hottest day of the year is easy to find. readings.sort_values("temperature_c", ascending=False).head(3) gives M019 on 2025-07-10 at 93.0 degrees.

A common mistake

readings["temperature_c"] and readings[["temperature_c"]] are not the same. One pair of brackets gives a Series. Two pairs give a DataFrame with one column. Most of the time you want the Series. If a command fails with a strange error, count your brackets first.