HomeCourseModule 09 › DataFrames — Python's spreadsheet

DataFrames — Python's spreadsheet

Module 09 · Pandas Crash Course9 min readBeginner

What you'll learn

  • Build a DataFrame from a dict or list of dicts
  • Use .head(), .info(), .describe(), .shape
  • Distinguish a DataFrame from a Series

Two ways to make a DataFrame

import pandas as pd

# From a dict of columns
df = pd.DataFrame({
    "name":  ["Alice", "Bob", "Carol"],
    "age":   [30, 25, 35],
    "city":  ["NYC", "Chicago", "LA"],
})

# From a list of rows (each row a dict)
rows = [
    {"name": "Alice", "age": 30, "city": "NYC"},
    {"name": "Bob",   "age": 25, "city": "Chicago"},
    {"name": "Carol", "age": 35, "city": "LA"},
]
df = pd.DataFrame(rows)

The four "what's in this thing?" methods

df.head()       # first 5 rows
df.tail(3)      # last 3 rows
df.shape        # (rows, columns)
df.info()       # column names, dtypes, missing counts
df.describe()   # numeric summary stats

Series vs DataFrame

A Series is a single column. A DataFrame is many columns aligned by row.

df["age"]           # a Series
type(df["age"])     # <class 'pandas.core.series.Series'>
df[["age", "city"]] # a DataFrame (note: double brackets)

Renaming columns

df = df.rename(columns={"name": "full_name"})
df.columns = ["full_name", "age", "city"]   # set all at once

Adding a column

df["is_adult"] = df["age"] >= 18
df["greeting"] = "Hi " + df["full_name"]

Walkthrough: from raw to summary in five lines

Read

df = pd.read_csv("sales.csv")
df.head()

Inspect

df.shape          # (1000, 6)
df.info()
df.describe()

Quick summary

df.groupby("region")["amount"].sum()

Key takeaways

  • DataFrame = table; Series = one column.
  • .head(), .info(), .describe(), .shape are the inspection workhorses.
  • Build from a dict-of-lists or a list-of-dicts.

Quick build

Create a DataFrame of five employees with name, department, salary. Print .describe() on it and notice what it shows for the numeric column versus the text ones.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.