0
0
NumPydata~15 mins

Slicing rows and columns in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Slicing rows and columns
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(selected_sales) to display the sliced array.