NumPy is the quiet foundation of data analysis in Python. Every time you compute a mean in pandas, filter with a boolean mask, or handle a missing value, NumPy is doing the work underneath. Learning it directly gives you a clearer picture of how pandas behaves and adds a few tools, like fast array math and conditional logic, that pandas alone does not cover as neatly. This tutorial introduces the essentials for an analyst.
The heart of NumPy is one object: the ndarray, a grid of numbers that all share a single data type. That uniformity is what lets NumPy run math on entire arrays at once, in compiled C, at speeds a Python loop cannot approach.
Why arrays beat lists
A Python list can hold anything and is flexible, but that flexibility makes numeric work slow. A NumPy array fixes the type and stores values compactly, so operations run fast and read cleanly. Import NumPy by its universal alias np:
import numpy as np
prices = np.array([100, 250, 80, 300])
print(prices * 1.1) # 10% markup on every element at once
print(prices.dtype)
[110. 275. 88. 330. ]
int64
Multiplying the whole array by 1.1 applied to every element with no loop. This is vectorization, and it is the single most important NumPy idea. With a plain list, prices * 1.1 would raise an error, and you would need a loop or comprehension.
Creating arrays
Beyond np.array, NumPy offers generators for common shapes, which you will see throughout data code:
print(np.zeros(3)) # array of zeros
print(np.arange(0, 10, 2)) # like range, as an array
print(np.linspace(0, 1, 5)) # 5 evenly spaced values 0..1
[0. 0. 0.]
[0 2 4 6 8]
[0. 0.25 0.5 0.75 1. ]
Arrays can be multi-dimensional too. A two-dimensional array is a grid of rows and columns, the same shape idea a DataFrame uses:
grid = np.array([[1, 2, 3], [4, 5, 6]])
print(grid.shape)
(2, 3)
The .shape attribute reports dimensions as (rows, columns), exactly like df.shape in pandas, because pandas borrows this from NumPy.
Vectorized math and aggregation
NumPy arrays support arithmetic and comparison element-wise, and carry built-in aggregation methods:
a = np.array([10, 20, 30, 40])
print(a + a) # element-wise addition
print(a.mean())
print(a.sum())
print(a.max())
[20 40 60 80]
25.0
100
40
These are the same aggregations you use in pandas, because a pandas Series stores its values in a NumPy array and delegates the math to it.
Indexing, slicing and boolean masks
Arrays index and slice like lists, but they add one feature that is central to pandas: the boolean mask. A comparison produces an array of True/False, which you then use to select elements:
scores = np.array([45, 88, 72, 95, 60])
mask = scores >= 70
print(mask)
print(scores[mask])
[False True True True False]
[88 72 95]
That is precisely how pandas filtering works, df[df["col"] > 70] is this exact mechanism one level up. Seeing it in raw NumPy demystifies what pandas is doing.
np.where for conditional values
One NumPy function every analyst should know is np.where, a vectorized if/else that builds a new array from a condition. It is invaluable for creating labelled or capped columns:
temps = np.array([18, 27, 31, 12, 25])
label = np.where(temps >= 25, "warm", "cool")
print(label)
['cool' 'warm' 'warm' 'cool' 'warm']
Inside pandas you use the same function on a column: df["band"] = np.where(df["salary"] >= 60000, "high", "standard"). It is faster and cleaner than an apply with a lambda for simple two-way conditions.
Practical usage
For an analyst, most NumPy usage is indirect, through pandas, but a handful of direct uses come up regularly. np.where builds conditional columns without a slow apply. np.nan is the standard missing-value marker pandas uses. Functions like np.log, np.sqrt and np.round apply vectorized math to columns for transformations and feature engineering. And when you feed data into a machine-learning library such as scikit-learn, it usually expects NumPy arrays, which pandas hands over via df.values or df.to_numpy(). Understanding arrays makes that hand-off feel natural rather than mysterious.
Common mistakes
- Expecting list behaviour from arrays.
array + arrayadds element-wise, whilelist + listconcatenates. The two look similar but behave very differently. - Mixing types in one array. An array of numbers and strings becomes all strings, silently breaking math. Keep each array to one intended type and check
.dtype. - Looping instead of vectorizing. Writing a Python
forloop over an array throws away NumPy's speed. Reach for vectorized operations ornp.wherefirst. - Confusing shape with size.
.shapegives dimensions as a tuple;.sizegives the total number of elements. Mixing them up leads to reshape errors.
In interviews
For data analyst roles, NumPy questions tend to be conceptual: "why is NumPy faster than a Python list?" where the answer is fixed types, compact storage and vectorized C operations. You may be asked to explain vectorization or to build a conditional column, where np.where is the crisp answer. Interviewers also like to hear that pandas is built on NumPy and that a Series wraps an ndarray, which shows you understand the stack rather than just the surface API. Even light NumPy fluency signals a solid foundation.
Where this fits in your learning path
NumPy is the foundation layer of the data analytics path. While pandas is where you spend most of your time, understanding arrays clarifies the pandas introduction and the internals of Series and DataFrame. The vectorization ideas here explain why the guidance in apply and map favours built-in operations over per-row functions. A working grasp of NumPy rounds out the technical base expected in the data analyst role.
Frequently Asked Questions
What is NumPy used for?
What is the difference between a NumPy array and a Python list?
Do I need NumPy if I know pandas?
What is vectorization in NumPy?
How do I create a NumPy array?
Want to Build Your Career in Data Analytics with AI?
Join CodeBegun and train with working industry engineers — Check the Data Analyst training details

