Pandas & NumPy Quick Reference
This reference is meant to be scanned, not read start to finish: it collects the syntax patterns you reach for constantly once you already understand the underlying concepts, grouped by task. Each snippet favors the idiomatic, vectorized form over a naive loop-based alternative, since idiomatic pandas/NumPy code is both faster and more readable once the patterns become familiar. Keep this page open in a second tab while working through real datasets, and revisit the fuller topic pages linked in 'related' whenever a snippet needs more context than a one-liner can provide.
Cricket analogy: This reference is like a bowler's cheat sheet of field placements you've already mastered - you don't re-learn the theory, you glance at it mid-match to recall the exact fielding pattern for a left-hander at the death.
NumPy Array Essentials
Array creation, shape inspection, and basic math cover the majority of day-to-day NumPy usage. np.array() converts a Python list/tuple; np.zeros(), np.ones(), and np.arange() generate arrays without manual literals; .shape, .dtype, and .ndim describe an array's structure at a glance. Element-wise operators (+, -, *, /, **) and universal functions (np.sqrt, np.exp, np.log) apply across the whole array without an explicit loop.
Cricket analogy: np.array() is like converting a handwritten scorecard list into an official scoreboard; np.zeros() sets every player's runs to 0 before a match starts; and applying np.sqrt() or np.log() to strike rates works across the whole team at once without looping player by player.
import numpy as np
# Creation
a = np.array([1, 2, 3, 4])
zeros = np.zeros((2, 3)) # 2x3 array of 0.0
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
linspace = np.linspace(0, 1, 5) # [0. , 0.25, 0.5 , 0.75, 1. ]
# Inspection
print(a.shape, a.dtype, a.ndim) # (4,) int64 1
# Indexing & slicing
first_two = a[:2] # [1, 2] (a view)
mask = a[a > 2] # [3, 4] (a copy, boolean indexing)
# Aggregation
print(a.sum(), a.mean(), a.std(), a.max())
# Reshaping
grid = np.arange(6).reshape(2, 3)Pandas Selection and Cleaning
Selecting subsets and cleaning messy input dominates real-world pandas usage far more than fancy transformations. .loc[] (label-based) and .iloc[] (position-based) cover almost all selection needs; boolean masks handle conditional filtering; and isna()/fillna()/dropna()/drop_duplicates() handle the most common cleaning tasks.
Cricket analogy: .loc[] pulls a batsman by name, .iloc[] pulls the third row of the scorecard by position; a boolean mask filters for 'centuries only', and isna()/fillna() patch a rain-interrupted match's missing overs before analysis.
import pandas as pd
df = pd.DataFrame({
'customer': ['A', 'B', 'C', 'D'],
'spend': [250.0, None, 180.5, 250.0],
'active': [True, True, False, True]
})
# Selection
df.loc[df['active'], 'customer'] # customers where active is True
df.iloc[0:2, 0:2] # first 2 rows, first 2 columns
df[df['spend'] > 200] # boolean filter
# Cleaning
df['spend'] = df['spend'].fillna(df['spend'].mean())
df = df.drop_duplicates(subset=['customer'])
df.isna().sum() # count of missing values per columnGrouping, Merging, and Reshaping
groupby() combined with .agg() is the workhorse for summarization; merge() combines related tables the way a SQL JOIN would; pivot_table() and melt() convert between wide and long layouts.
Cricket analogy: groupby().agg() is like summarizing every batsman's season average in one pass; merge() joins the batting and bowling scorecards the way a match report combines both innings; pivot_table()/melt() convert a wide season table into a long ball-by-ball log or back.
import pandas as pd
orders = pd.DataFrame({'region': ['N','N','S','S'], 'amount': [100, 150, 90, 60]})
customers = pd.DataFrame({'region': ['N','S'], 'manager': ['Ana', 'Ravi']})
# Grouping
summary = orders.groupby('region').agg(total=('amount', 'sum'), avg=('amount', 'mean'))
# Merging (like a SQL JOIN)
merged = orders.merge(customers, on='region', how='left')
# Reshape wide -> long
wide = pd.DataFrame({'id': [1, 2], 'jan': [10, 20], 'feb': [15, 25]})
long = wide.melt(id_vars='id', var_name='month', value_name='value')
# Reshape long -> wide
pivoted = long.pivot(index='id', columns='month', values='value')Almost every fast pandas/NumPy operation shares one property: it operates on the whole array or column at once rather than element-by-element in a Python loop. If you find yourself writing for i in range(len(df)), pause and look for the vectorized equivalent in this reference first.
This page intentionally omits explanations of *why* each pattern works — it assumes you've already read the dedicated topic pages. Using a snippet here without understanding its edge cases (e.g. merge()'s default join type, .loc vs .iloc slicing boundaries) is a common source of subtle bugs.
- NumPy creation:
np.array(),np.zeros(),np.arange(),np.linspace(); inspect with.shape,.dtype,.ndim. - Pandas selection:
.loc[]for labels,.iloc[]for positions, boolean masks for conditions. - Cleaning:
isna(),fillna(),dropna(),drop_duplicates(subset=...). - Summarization:
groupby(...).agg(...)for grouped statistics with named output columns. - Combining:
merge(on=..., how=...)for SQL-style joins between DataFrames. - Reshaping:
melt()for wide-to-long,pivot()/pivot_table()for long-to-wide.
Practice what you learned
1. Which NumPy function generates evenly spaced values by specifying a total count rather than a step size?
2. Which pandas method removes duplicate rows based only on specific columns?
3. What does `orders.groupby('region').agg(total=('amount', 'sum'))` produce?
4. Which function converts a wide DataFrame (one column per category) into long format (one row per category-value pair)?
5. In `orders.merge(customers, on='region', how='left')`, what determines which rows are kept?
Was this page helpful?
You May Also Like
Pandas & NumPy Interview Questions
A curated set of frequently asked pandas and NumPy interview questions with precise answers, covering indexing, performance, missing data, and common gotchas.
Common Pandas & NumPy Pitfalls
A field guide to the mistakes that trip up even experienced users of pandas and NumPy — chained assignment, dtype surprises, mutable defaults, and silent misalignment.
Indexing with loc and iloc
Master the two primary pandas selection accessors — .loc for label-based indexing and .iloc for integer-position-based indexing — and the pitfalls of chained indexing.
GroupBy Basics
Understand pandas' split-apply-combine model via groupby(), including grouping keys, selecting columns, and iterating over groups.