📖 Scenario: You work in a small grocery store. You have a list of daily sales data for different products. You want to look at specific parts of this data to understand sales better.
🎯 Goal: Learn how to slice rows and columns from a NumPy array to select specific parts of the sales data.
📋 What You'll Learn
Create a NumPy array with given sales data
Create variables to select specific rows and columns
Use slicing to extract parts of the array
Print the sliced parts to see the result
💡 Why This Matters
🌍 Real World
Slicing rows and columns helps you focus on parts of large datasets, like sales data, to analyze specific time periods or products.
💼 Career
Data scientists and analysts often slice data arrays to prepare data for reports, visualizations, or machine learning.
Progress0 / 4 steps
1
Create the sales data array
Import NumPy as np and create a 2D NumPy array called sales with these exact values: [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]].
NumPy
Hint
Use np.array() to create the array with the exact nested list.
2
Set row and column slice variables
Create a variable called row_slice that selects rows 1 and 2 (second and third rows) and a variable called col_slice that selects columns 0 and 1 (first and second columns). Use Python slice notation.
NumPy
Hint
Remember, slice(start, stop) selects rows or columns from start up to but not including stop.
3
Slice the sales array using the variables
Create a new variable called selected_sales by slicing the sales array using row_slice for rows and col_slice for columns.
NumPy
Hint
Use sales[row_slice, col_slice] to slice rows and columns at the same time.
4
Print the sliced sales data
Print the variable selected_sales to see the sliced part of the sales data.
NumPy
Hint
Use print(selected_sales) to display the sliced array.
Practice
(1/5)
1. What does the slicing notation array[1:3, 0:2] do on a 2D NumPy array?
easy
A. Selects rows 1 and 2, and columns 0 and 1
B. Selects rows 0 to 3 and columns 0 to 2
C. Selects rows 1 to 3 and columns 0 to 2
D. Selects rows 1 and 3, and columns 0 and 2
Solution
Step 1: Understand row slicing 1:3
This means select rows starting at index 1 up to but not including index 3, so rows 1 and 2.
Step 2: Understand column slicing 0:2
This means select columns starting at index 0 up to but not including index 2, so columns 0 and 1.
Final Answer:
Selects rows 1 and 2, and columns 0 and 1 -> Option A
Quick Check:
Slice end is excluded = A [OK]
Hint: Remember end index is excluded in slicing [OK]
Common Mistakes:
Including the end index in the slice
Confusing rows and columns order
Using commas incorrectly in slicing
2. Which of the following is the correct syntax to select all rows and columns 2 to 4 (inclusive start, exclusive end) from a NumPy array named arr?
easy
A. arr[2:4, :]
B. arr[:, 2:5]
C. arr[2:5, :]
D. arr[:, 2:4]
Solution
Step 1: Identify rows and columns in slicing
The first index before comma is rows, second is columns.
Step 2: Select all rows and columns 2 to 4
All rows means :, columns 2 to 4 means 2:5 because end is excluded.