Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pandas

A library for data manipulation and analysis.

  • Series: a one-dimensional labeled array.
  • DataFrame: a two-dimensional labeled data structure with columns of potentially different types.
import pandas as pd

Series

pd.Series([10, 20, 30, 40])         # from a python list
pd.Series({"a": 1, "b": 2, "c": 3}) # from a python dict

DataFrame Creation

# from a dict of lists (keys become columns)
data = {
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [24, 30, 22],
    "City": ["Sydney", "Melbourne", "Brisbane"],
}
df = pd.DataFrame(data)

# from a List-of-Dict (each dict becomes a row)
rows = [
    {"Name": "Alice", "Age": 24, "City": "Sydney"},
    {"Name": "Bob", "Age": 30, "City": "Melbourne"},
]
df = pd.DataFrame(rows)

Inspection

df.head()     # first 5 rows
df.tail()     # last 5 rows
df.info()     # columns, dtypes, non-null counts
df.describe() # numeric summary (count, mean, std, min, max, ...)

df.shape      # (rows, cols)
df.columns    # column labels
df.dtypes     # column dtypes

Selection

df["Name"]            # select a column (a Series)
df[["Name", "City"]]  # select multiple columns (a DataFrame)
df.iloc[0]            # select row by position (integer)
df.loc[0]             # select row by index label
df[df["Age"] > 23]    # filter rows by condition (boolean mask)

Operations

# add a column, derive from existing ones
df["Age Next Year"] = df["Age"] + 1

df["Age"].mean()        # column mean
df["Age"].max()         # column max
df["Name"].str.upper()  # string methods on column values

# grouping
df.groupby("City")["Age"].mean()  # mean age per city