Data AnalyticsPandas & NumPybeginner
Updated:

NumPy Tutorial: Arrays for Data Analysis

4 min read

NumPy is the array library beneath pandas. Learn to create arrays, do vectorized math, index and slice, and compute aggregations, with runnable examples.

TL;DR – Quick Answer

NumPy is the Python library for fast numerical computing, built around the ndarray, a fixed-type, multi-dimensional array. Its key advantage is vectorized operations that apply math to a whole array at once in optimized C, far faster than Python loops. Pandas is built on top of NumPy, so understanding arrays clarifies how pandas works underneath. You import it as np.

On This Page

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 + array adds element-wise, while list + list concatenates. 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 for loop over an array throws away NumPy's speed. Reach for vectorized operations or np.where first.
  • Confusing shape with size. .shape gives dimensions as a tuple; .size gives 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?
NumPy is used for fast numerical computing on arrays of numbers, including vectorized arithmetic, linear algebra, statistics and random number generation. It is the foundation of the scientific Python stack, and libraries like pandas, scikit-learn and matplotlib are built on it. Analysts mostly meet it indirectly through pandas but use it directly for numeric work.
What is the difference between a NumPy array and a Python list?
A NumPy array holds one fixed data type and stores values compactly, which makes math on it far faster than on a list. Arithmetic on an array is vectorized, so array * 2 doubles every element at once, whereas a list would need a loop. Lists are flexible and can mix types, but they are slow for numeric work.
Do I need NumPy if I know pandas?
You can do a lot of pandas without touching NumPy directly, because pandas wraps it. But knowing NumPy helps you understand vectorization, handle NaN, use np.where for conditional columns, and work with array-based libraries. A little NumPy makes you a more capable pandas user.
What is vectorization in NumPy?
Vectorization means applying an operation to an entire array at once instead of looping element by element. NumPy runs these operations in compiled C code, so they are much faster than equivalent Python loops. This is why vectorized pandas and NumPy code is preferred over manual iteration.
How do I create a NumPy array?
Call np.array([1, 2, 3]) to build one from a list, or use generators like np.zeros(5), np.arange(0, 10) and np.linspace(0, 1, 5). Each returns an ndarray with a single dtype. You can check its shape and dtype with the .shape and .dtype attributes.

Want to Build Your Career in Data Analytics with AI?

Join CodeBegun and train with working industry engineers — Check the Data Analyst training details

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 16 July 2026 LinkedIn
Chat with us