Indexing, Slicing & Filtering

In data analytics, you rarely work with the entire dataset at once.

Most of the time, you need to access specific values, extract subsets, or filter data based on conditions.

NumPy provides powerful and flexible ways to do this using :

Mastering these concepts is essential for real-world data analysis.



What Is Indexing?

Indexing in NumPy refers to the process of accessing individual elements or specific positions within an array. You can access elements using their position (index) in the array, starting from 0 for the first element.

NumPy uses zero-based indexing, just like Python lists.


Indexing in 1D Arrays

import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[0])   # First element
print(arr[2])   # Third element
print(arr[-1])  # Last element

Explanation

This is useful when you want specific data points, such as the latest value.


Indexing in 2D Arrays

In 2D arrays, indexing follows this format :

array[row_index, column_index]

Example

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(matrix[0, 1])  # Element from first row, second column

Real-Life Example: Student Scores

scores = np.array([
    [85, 90, 88],
    [78, 82, 80],
    [92, 95, 94]
])

print(scores[2, 1])  # Second subject score of third student

Indexing helps extract exact values from structured data.



What Is Slicing?

Slicing in NumPy allows you to extract a portion of an array using start, stop, and step parameters. It works similarly to Python list slicing.

The syntax for slicing is:

array[start:stop:step]

Slicing in 1D Arrays

import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])   # Elements from index 1 to 3
print(arr[:3])    # First three elements
print(arr[2:])    # Elements from index 2 onwards
print(arr[::2])   # Every second element

Slicing in 2D Arrays

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

print(matrix[:2, :2])  # First two rows and first two columns
print(matrix[1:, 1:])  # From second row and second column onwards

Real-Life Example: Monthly Sales Data

sales = np.array([
    [1000, 1200, 1100],
    [1300, 1250, 1400],
    [1500, 1600, 1550]
])

print(sales[1:3, 0:2])  # Sales data for months 1 and 2 of years 2 and 3
print(sales[0:2, :])    # Sales data for all months of years 1 and 2

Slicing is commonly used for time-based analysis.



What Is Filtering?

Filtering in NumPy involves selecting elements from an array based on a condition or boolean mask. This is particularly useful when you want to extract specific data points that meet certain criteria.

This is one of the most powerful features of NumPy.

The syntax for filtering is :

array[condition]

Filtering in 1D Arrays

import numpy as np
arr = np.array([10, 20, 30, 40, 50])
filtered_arr = arr[arr > 25]   # Elements greater than 25
print(filtered_arr)

Filtering in 2D Arrays

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

filtered_matrix = matrix[matrix > 5]   # Elements greater than 5
print(filtered_matrix)

Real-Life Example: Student Scores

scores = np.array([
    [85, 90, 88],
    [78, 82, 80],
    [92, 95, 94]
])

high_scores = scores[scores > 90]   # Scores greater than or equal to 90
print(high_scores)

Useful when :


Multiple Conditions in Filtering

arr = np.array([10, 20, 30, 40, 50])

result = arr[(arr > 20) & (arr < 50)]
print(result)

Use :


Filtering in 2D Arrays

data = np.array([
    [80, 85, 90],
    [60, 65, 70],
    [95, 92, 88]
])
print(data[data > 85])

This extracts all values greater than 85 from the entire array.



Common Beginner Mistakes

Avoiding these mistakes improves accuracy.



Why Indexing, Slicing & Filtering Are Important

These techniques help you :

Almost every real-world data task involves filtering and slicing.



Key Takeaways



Conclusion

Indexing, slicing, and filtering are fundamental skills in NumPy that allow you to manipulate and analyze data effectively. Mastering these techniques is crucial for any data analyst or scientist working with NumPy arrays.