Lesson 6 · Data analytics zero to hero
NumPy Arrays & Operations for Beginners | Data Analytics #6
And the start of the tools block: NumPy, the array library nearly every other data tool in Python is built on top of. We'll load real numeric columns from…
- CourseData analytics zero to hero
- Lesson6 of 30
- Video14 min
- FormatJupyter notebook · 13 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- mtcars.csv preview1.7 KB
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 6: NumPy#
- Video six of the 30-part series, and the start of the tools block: NumPy, the array library nearly every other data tool in Python is built on top of.
- We'll load real numeric columns from mtcars, the 1974 Motor Trend car road test data, into NumPy arrays.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
- Install NumPy if you haven't already:
pip install numpy. - Place mtcars.csv in the same folder as this notebook.
Part 1: Why NumPy? Arrays vs. Lists#
import numpy as np
mpg_list = [21.0, 22.8, 21.4, 18.7, 18.1]
mpg_array = np.array(mpg_list)
print(mpg_array)
print(type(mpg_array))
print(mpg_list * 2)
print(mpg_array * 2)
Part 2: Creating Arrays#
zeros = np.zeros(5)
ones = np.ones(5)
sequence = np.arange(0, 10, 2)
evenly_spaced = np.linspace(0, 1, 5)
print(zeros)
print(ones)
print(sequence)
print(evenly_spaced)
grid = np.array([[1, 2, 3], [4, 5, 6]])
print(grid)
print(grid.shape)
print(grid.ndim)
Part 3: Indexing and Slicing#
mpg_array = np.array([21.0, 22.8, 21.4, 18.7, 18.1, 14.3])
print(mpg_array[0])
print(mpg_array[-1])
print(mpg_array[1:4])
print(mpg_array[:3])
grid = np.array([[21.0, 6, 160], [22.8, 4, 108], [21.4, 6, 258]])
print(grid[0, 1])
print(grid[:, 0])
print(grid[1, :])
print(mpg_array[mpg_array > 20])
Part 4: Vectorized Math and Aggregation#
weights_1000lb = np.array([2.62, 2.875, 2.32, 3.215, 3.44])
weights_lb = weights_1000lb * 1000
print(weights_lb)
print(weights_lb + 50)
mpg_array = np.array([21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 32.4])
print(mpg_array.mean())
print(mpg_array.std())
print(mpg_array.min())
print(mpg_array.max())
print(mpg_array.sum())
print(np.round(mpg_array.mean(), 2))
print(np.sort(mpg_array))
print(np.median(mpg_array))
Part 5: Loading a Real CSV Column with NumPy#
mpg_column = np.genfromtxt('mtcars.csv', delimiter=',', skip_header=1, usecols=1)
print(mpg_column[:5])
print(mpg_column.shape)
print(mpg_column.mean())
hp_column = np.genfromtxt('mtcars.csv', delimiter=',', skip_header=1, usecols=4)
efficient_mask = mpg_column > 20
print(hp_column[efficient_mask].mean())
print(hp_column[~efficient_mask].mean())
Part 6: Reshaping Arrays#
flat = np.arange(12)
print(flat)
grid = flat.reshape(3, 4)
print(grid)
print(grid.reshape(-1))
Wrap-Up: What You Learned#
- Why NumPy arrays beat plain lists for numeric work: fixed types and vectorized, element-wise math.
- Creating arrays with array, zeros, ones, arange, and linspace.
- Indexing and slicing, in one and two dimensions, plus boolean masking.
- Aggregation functions: mean, std, min, max, sum, sort, and median.
- Loading a real CSV column directly into NumPy with genfromtxt, and combining masks across two real columns.
- Reshaping arrays with reshape.
- NumPy is the engine under the hood of pandas, which is exactly where we're headed. Video seven starts a multi-part pandas block with Series and DataFrames. Subscribe so it lands automatically see you there.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What is the difference between
mpg_list * 2andmpg_array * 2, for a Python list and a NumPy array?Lists treat * as repetition, while NumPy arrays do element-wise (vectorized) math.
What does
np.arange(0, 10, 2)produce?arange(start, stop, step) stops before the stop value, so 0, 2, 4, 6, 8.
For
grid = np.array([[21.0, 6, 160], [22.8, 4, 108], [21.4, 6, 258]]), what isgrid[:, 0]?:means all rows, and 0 picks the first column.What does
mpg_array[mpg_array > 20]do?This is boolean masking: the comparison creates a True/False array, and indexing with it keeps only the True positions.
What is the shape of
np.arange(12).reshape(3, 4)?reshape(3, 4) makes 3 rows and 4 columns.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Create an array from
[21.0, 22.8, 21.4, 18.7, 18.1, 14.3]. Print its mean, min, max and the values above 20.💡 Hint
Use .mean(), .min(), .max() and a boolean mask.
✅ Show solution
import numpy as np mpg = np.array([21.0, 22.8, 21.4, 18.7, 18.1, 14.3]) print(mpg.mean(), mpg.min(), mpg.max()) print(mpg[mpg > 20])
Convert the weights
[2.62, 2.875, 2.32, 3.215, 3.44](1000 lbs) to pounds, then print the sorted weights and their median.💡 Hint
Multiply the array by 1000, then np.sort and np.median.
✅ Show solution
import numpy as np weights = np.array([2.62, 2.875, 2.32, 3.215, 3.44]) pounds = weights * 1000 print(np.sort(pounds)) print(np.median(pounds))
Given
mpg = np.array([21, 22.8, 18.7, 14.3, 24.4, 15.2])andhp = np.array([110, 93, 175, 245, 62, 150]), compute the average hp of cars with mpg above 20 and the average hp of the rest. Then reshapenp.arange(6)into 2 rows by 3 columns.💡 Hint
Create mask = mpg > 20, use ~mask for the opposite.
✅ Show solution
import numpy as np mpg = np.array([21, 22.8, 18.7, 14.3, 24.4, 15.2]) hp = np.array([110, 93, 175, 245, 62, 150]) mask = mpg > 20 print(hp[mask].mean()) # efficient cars print(hp[~mask].mean()) # the rest print(np.arange(6).reshape(2, 3))
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



