NumPy Arrays
Fast numerical computing with NumPy arrays — the foundation under pandas
NumPy (Numerical Python) is the library that powers pandas under the hood. NumPy arrays (ndarray) are like Python lists but much faster for mathematical operations — because operations run on the entire array at once, not element by element.
As a data analyst, you will use NumPy for: fast column-level math, generating test data, applying mathematical functions, and creating arrays for charts. You rarely use NumPy directly when pandas can do the job, but understanding it makes you a better pandas user.
Examples
Key Points
- ✓NumPy arrays are faster than Python lists for math — operations run on the whole array at once
- ✓arr.shape gives dimensions; arr.dtype gives the data type
- ✓Boolean masking: arr[arr > 60000] filters elements where condition is True
- ✓np.where(cond, a, b) — vectorised if-else, much faster than apply()
- ✓pandas Series and DataFrame columns are backed by NumPy arrays internally
Practice Question
What does arr * 2 do when arr = np.array([10, 20, 30])?