NumPy Arrays
Fast numerical computation with NumPy arrays — the engine behind Pandas
NumPy (Numerical Python) is the foundation of the entire Python data science stack. Pandas, Matplotlib, and scikit-learn are all built on NumPy. NumPy arrays are like Python lists but dramatically faster for numerical operations — they store data in contiguous memory and apply operations to entire arrays at once (vectorisation).
For data analysts, you rarely use NumPy directly — Pandas handles most tasks. But understanding NumPy arrays helps you write faster Pandas code and understand why certain operations are fast or slow.
Examples
Key Points
- ✓import numpy as np — the universal convention
- ✓NumPy arrays are faster than Python lists because of contiguous memory and vectorisation
- ✓All elements in a NumPy array must be the same dtype
- ✓Boolean indexing: arr[arr > 20000] filters elements matching the condition
- ✓Pandas Series is built on NumPy array — .values returns the underlying NumPy array
Practice Question
What does sales[sales > 20000] return if sales = np.array([45000, 18000, 62000, 9000])?