Pandas for Data Science
Why NumPy alone isn't enough
NumPy arrays are fast, but they're also rigid: every element shares one data type, and there's no built-in concept of column names, row labels, or missing values. Real-world data almost never looks like that. A customer dataset has names (strings), ages (integers), purchase amounts (floats), and signup dates (timestamps) all in one table, with gaps where information is missing.
Pandas is built on top of NumPy specifically to handle this kind of labeled, mixed-type, imperfect data. Its core object, the DataFrame, is essentially a table: rows and columns, each column with its own data type, both rows and columns addressable by label rather than just position — and a huge library of operations for cleaning, reshaping, and summarizing that data.
groupby: the single most useful operation
groupby answers questions of the form "for each category, what's the summary?" — total revenue by region, average purchases per customer segment, count of events per day. The pattern is always the same three steps: split the data into groups based on some key, apply an aggregation function to each group independently, then combine the results back into a single table. This split-apply-combine pattern underlies the large majority of real exploratory data analysis, and it's the pandas operation worth knowing cold before anything else.
merge: combining data from multiple tables
Real datasets are rarely a single table — customer info lives in one table, orders in another, product details in a third. merge (pandas' equivalent of a SQL join) combines rows from two DataFrames based on matching values in one or more key columns. An inner merge keeps only rows with a match in both tables; a left merge keeps every row from the left table, filling in missing values where there's no match on the right — the choice between them is a real modeling decision, not a technicality, since it determines whether unmatched rows silently disappear or stay in with gaps.
Note
len(merged) against your expectation immediately after a merge, especially before feeding the result into any downstream aggregation or model training step.pivot_table: reshaping long data into a summary grid
pivot_table takes long, row-per-observation data and reshapes it into a grid — one category across the columns, another down the rows, and an aggregated value filling each cell. It's effectively a groupby on two keys at once, displayed as a cross-tabulated table rather than a flat result, which is exactly the shape many business reports and quick visual summaries need.
Core pandas operations and when to reach for each
| Operation | Purpose | Typical question it answers |
|---|---|---|
| groupby | Aggregate rows by category | What's the average/total per group? |
| merge | Combine two related tables | How do these two datasets connect? |
| pivot_table | Reshape long data into a summary grid | How does metric X vary across two categories at once? |
| dropna / fillna | Handle missing values | What do I do about gaps in the data? |
What's next
Cleaning and summarizing data with pandas gets you numbers, but numbers alone don't reveal patterns, outliers, or bugs as quickly as a plot does. Continue to Data Visualization for ML with Matplotlib to see visualization used as an active debugging tool throughout the ML workflow, not just a reporting step at the end.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.