Pandas
Beginner 12 min read Pandas

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.

SeriesDataFramepandastableindexlabeled data
Prerequisites: NumPy Introduction
Before we start

What you will learn

  • Define Series and DataFrame
  • Read a small table and inspect it
  • Explain how labels replace positional thinking
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A spreadsheet with named columns and a row-numbered grid

Think of

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.

Mechanics

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.

Columns
Index
Cells
Selection
Transforms
Hands-on

Examples

From data to analysis in five lines

From data to analysis in five lines
import pandas as pd
df = pd.DataFrame({
"user": ["a", "b", "a", "c"],
"revenue": [10, 20, 30, 40],
})
print(df["revenue"].mean()) # 25.0
print(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.

Real world

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.
Guidelines

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.
Watch out

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.
When things break

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.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

In pandas, `df['revenue']` returns:

2

Pandas column selection uses labels, numpy indexing uses positions.

Go deeper

Official documentation