What Is pandas? β Definition and How It Works
Learn what pandas is, how data frames and series work, the operations that matter for cleaning collected data, and where it stops scaling.
pandas is Python's standard library for tabular data. It provides labelled, columnar structures and the operations β filtering, grouping, joining, reshaping β that turn collected records into an analysable table.
1. What Is pandas?
A library for working with tables in memory.
- Key idea: columns are typed arrays with labels, and operations apply to whole columns at once.
- Mechanism: a
DataFrameofSeriesobjects backed by contiguous arrays. - Goal: express data cleaning and analysis concisely, without per-row loops.
Example
import pandas as pd
df = pd.DataFrame(records)
df["price"] = pd.to_numeric(df["price"].str.replace(r"[^\d.]", "", regex=True))
top = df[df["rating"] >= 4].sort_values("price").head(10)
2. The Two Structures
| Structure | What it is |
|---|---|
Series |
One labelled column with a single dtype |
DataFrame |
An ordered collection of Series sharing an index |
The index is the row label. It can be a default integer range, a timestamp, or a key β and it participates in joins and alignment, which is a frequent source of surprise for newcomers.
3. Operations That Matter for Collected Data
- Type coercion β
to_numeric,to_datetime, witherrors="coerce"turning unparseable values intoNaNinstead of raising. - String cleaning β the
.straccessor applies string methods across a column. - Missing values β
isna,fillna,dropna, and an explicit decision about what absence means. - Deduplication β
drop_duplicateson the subset of columns that define identity. - Grouping β
groupbywith an aggregation, for per-category summaries. - Joining β
mergefor key-based joins,concatfor stacking.
4. Reading and Writing
pandas reads and writes CSV, JSON, JSON Lines, Parquet, Excel, SQL tables, and HTML tables. Two are worth singling out for collected data:
read_htmlextracts every table on a page into data frames in one call.- Parquet preserves types and compresses well, which matters once a dataset outgrows CSV round-tripping.
5. Vectorise Rather Than Loop
Column-wide operations run in compiled code; per-row Python loops do not. The idiomatic form is usually both faster and shorter.
apply sits between the two: convenient, but still executing Python per row. Reach for built-in vectorised operations first and treat apply as the fallback.
6. Where pandas Stops
Everything happens in memory, and intermediate copies can multiply peak usage well beyond the file size on disk. Once a dataset stops fitting comfortably, the options are chunked processing, a columnar format with predicate pushdown, or a library designed for larger-than-memory work.
7. Common Misconceptions
- "pandas is a database." It has no durability, transactions, or concurrent access.
- "The index is decoration." It drives alignment during joins and arithmetic; misaligned indexes produce silent
NaN. - "
NaNmeans zero." It means unknown, and treating it as zero changes aggregates. - "Chained assignment is fine." Assigning through a chained selection can write to a copy; use
.locfor assignment.
8. Summary
pandas gives Python labelled columnar tables and the vectorised operations needed to clean, join, group, and export them, with readers and writers for the formats collected data usually arrives in.
Its strengths are expressiveness and breadth of I/O. Its boundary is memory β and its most common source of subtle bugs is index alignment.