🔵 Data Science · Lesson 36
NumPy Indexing, Slicing and Operations
What is NumPy Indexing and Slicing?
NumPy Indexing and Slicing means indexing and slicing select specific values, rows, columns or ranges from NumPy arrays.
In real programs, this topic helps in selecting array rows and columns. Learn the idea first, then type the program yourself and compare the output.
💡 At a Glance
| Point | Details |
|---|---|
| Course Area | Data Science Tools and concepts used to analyse, clean and present data. |
| Main Use | selecting array rows and columns |
| Example File | numpy-indexing.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for selecting array rows and columns.
- It connects with filtering numbers.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| index | Labels used to identify rows. |
| slice | slice is an important term in this topic. |
| 2D array | 2D array is an important term in this topic. |
| row | Horizontal record in a table or array. |
| column | Vertical field in a table or array. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
Basic Pattern
import numpy as np
data = np.array([[10, 20, 30], [40, 50, 60]])
print("First row:", data[0])
print("Second column:", data[:, 1])
print("Value:", data[1, 2])Complete Example Program
Python – numpy-indexing.py
import numpy as np
data = np.array([[10, 20, 30], [40, 50, 60]])
print("First row:", data[0])
print("Second column:", data[:, 1])
print("Value:", data[1, 2])Expected Output
First row: [10 20 30]
Second column: [20 50]
Value: 60
Program Explanation
import numpy as npimports ready-made features from a module/library.data = np.array([[10, 20, 30], [40, 50, 60]])stores a value in data.print("First row:", data[0])displays information or calculated result on the screen.print("Second column:", data[:, 1])displays information or calculated result on the screen.print("Value:", data[1, 2])displays information or calculated result on the screen.
Where will you use it?
- Selecting array rows and columns.
- Filtering numbers.
- Working with image pixels.
Common Mistakes
- Analysing data before checking missing values, duplicates and data types.
- Changing original data without keeping a clean copy.
- Creating charts without title, labels or explanation.
Practice Tasks
- Type the program in
numpy-indexing.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to selecting array rows and columns.
- Write 5 lines explaining the logic in your own words.
Summary
NumPy Indexing and Slicing is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.