0
0
NumPydata~10 mins

Slicing rows and columns in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Slicing rows and columns
Start with 2D array
Choose row slice: start:end
Choose column slice: start:end
Extract sub-array
Use or display sliced data
We start with a 2D array, select rows by slicing, then select columns by slicing, and get a smaller sub-array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
slice_rows = arr[0:2, :]
slice_cols = arr[:, 1:3]
slice_rows_cols = arr[0:2, 1:3]
This code slices the first two rows and all columns, then slices all rows and columns 1 to 3, then slices rows 0:2 and columns 1:3 combined.
Execution Table
StepOperationRow sliceColumn sliceResulting array
1Original arrayallall[[1 2 3] [4 5 6] [7 8 9]]
2Slice rows 0 to 20:2all[[1 2 3] [4 5 6]]
3Slice columns 1 to 3all1:3[[2 3] [5 6] [8 9]]
4Slice rows 0 to 2 and columns 1 to 30:21:3[[2 3] [5 6]]
5Exit--Slicing complete
💡 Slicing stops after extracting desired rows and columns.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
arr[[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]]
slice_rowsN/A[[1 2 3] [4 5 6]][[1 2 3] [4 5 6]][[1 2 3] [4 5 6]][[1 2 3] [4 5 6]]
slice_colsN/AN/A[[2 3] [5 6] [8 9]][[2 3] [5 6] [8 9]][[2 3] [5 6] [8 9]]
slice_rows_colsN/AN/AN/A[[2 3] [5 6]][[2 3] [5 6]]
Key Moments - 3 Insights
Why does arr[0:2, :] select the first two rows and all columns?
Because 0:2 means rows starting at index 0 up to but not including 2, and ':' means all columns. See execution_table row 2.
What does arr[:, 1:3] do to the array?
It selects all rows (':') and columns from index 1 up to but not including 3. See execution_table row 3.
How do you slice both rows and columns at the same time?
Use arr[row_slice, column_slice], for example arr[0:2, 1:3] slices rows 0 and 1 and columns 1 and 2. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of slicing rows 0 to 2?
A[[1 2 3] [4 5 6]]
B[[2 3] [5 6]]
C[[7 8 9]]
D[[1 2 3] [4 5 6] [7 8 9]]
💡 Hint
Check execution_table row 2 under 'Resulting array'
At which step does the slicing select columns 1 to 3 for all rows?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at execution_table row 3 for column slice 1:3
If you want to slice only the last column for all rows, how would the slicing look?
Aarr[0:2, 0:1]
Barr[2:3, :]
Carr[:, 2:3]
Darr[1:3, 1:3]
💡 Hint
Column slice 2:3 means column index 2 only, see how column slices work in execution_table rows 3 and 4
Concept Snapshot
Slicing rows and columns in numpy:
Use arr[row_start:row_end, col_start:col_end]
Row slice selects rows from start up to but not including end
Column slice selects columns similarly
':' means select all rows or columns
Result is a smaller sub-array
Full Transcript
We start with a 2D numpy array. To slice rows, we use arr[start:end, :], where start and end define the row range. To slice columns, we use arr[:, start:end]. Combining both, arr[row_start:row_end, col_start:col_end] extracts a sub-array. The slices include the start index but exclude the end index. This lets us pick parts of the array easily.