Pandas Introduction
Labeled, table-shaped numpy
Pandas builds labeled tables (Series and DataFrame) on top of numpy, giving SQL-ish labeling, powerful indexing and method-chaining analytics without leaving Python.
What you will learn
- Define Series and DataFrame
- Read a small table and inspect it
- Explain how labels replace positional thinking
What is this?
Pandas is a table library. The Series is a labeled 1D array (numpy backing + an index); the DataFrame is a 2D labeled table (columns with names and dtypes, rows with an index). Its power is labeled alignment — operations line up by index label, not position — plus a sweeping API for selection, cleaning, grouping and IO.
Why do you need it?
- Real data arrives as tables with headers and dtypes, which numpy alone handles clumsily.
- Labeled operations (join on keys, `df['revenue']`) are readable and error-resistant.
- The groupby-apply pattern replaces dozens of hand-written loop/aggregate blocks.
- Pandas reads/writes CSV, Excel, Parquet, SQL and JSON — the interchange layer of DS.
Mental model
A spreadsheet with named columns and a row-numbered grid
A pandas DataFrame
You ask 'what's in the Revenue column?' by name, and formulas reference cells by address. pandas merges both: names up top, labels on the side, and operations that align formulas by name.
How it works
- Series = numpy array + an index (default RangeIndex) — labeled 1D values.
- DataFrame = dict of named Series sharing an index — the labeled table.
- df[col] selects a column (Series); df[[a, b]] selects several columns.
- Operations align on the index/column names ('label alignment'), a numpy-positional mindset must unlearn.
- Method chaining: df.dropna().groupby('x').mean() builds pipelines expression-wise.
Anatomy of a DataFrame.
Examples
From data to analysis in five lines
import pandas as pddf = pd.DataFrame({ "user": ["a", "b", "a", "c"], "revenue": [10, 20, 30, 40],})print(df["revenue"].mean()) # 25.0print(df.groupby("user")["revenue"].sum().sort_values(ascending=False))print(df[df["revenue"] > 15])One table, then mean, groupby-sum and a filter — each reads like the English instruction. The groupby returns a labeled Series indexed by user, which is the classic analysis shape.
In production
- An analytics team loads a daily partition CSV, validates dtypes, and pushes a cleaned Parquet onward.
- A fraud workflow selects flagged rows via a boolean mask and rolls them into a reporting frame.
- A migration script rewrites legacy Excel into tidy long-format DataFrames for the warehouse.
Best practices
- Think in columns and labels, not cells.
- Inspect with .head(), .info(), .dtypes, .shape before any transform.
- Chain transformations; keep a raw copy before destructive steps.
Common mistakes
- Using positional indexing (df[0]) when you meant column names — df[0] is a column, not a row, in pandas.
- Assuming operations are position-based; index alignment is the rule.
- Setting with chained assignment (df[a][b] = x) — never reliable.
Debugging tips
- df.info() shows dtypes + non-null counts — the first stop for 'why is my column weird'.
- If a join/scalar op surprises you, inspect the index — misaligned labels are the #1 gotcha.
- df.equals and idxmin for label-based comparisons verify operations.
Interview checkpoint
Quick quiz
In pandas, `df['revenue']` returns:
Pandas column selection uses labels, numpy indexing uses positions.