🎯 Array Indexing

Array indexing is your gateway to accessing and manipulating specific elements or sections of your data! Master basic indexing, advanced selection, and boolean filtering for powerful data analysis.

import numpy as np

# Array indexing overview
data = np.array([[10, 20, 30, 40],
                 [50, 60, 70, 80],
                 [90, 100, 110, 120]])

print(f"Array: \n{data}")

# Basic indexing examples
print(f"Single element [1, 2]: {data[1, 2]}")
print(f"First row: {data[0]}")
print(f"Last column: {data[:, -1]}")

🔍 Why Array Indexing Matters

Array indexing enables:

  • Data access 📊: Get exactly the elements you need
  • Data filtering 🔍: Extract data based on conditions
  • Data modification ✏️: Change specific values or regions
  • Memory efficiency 💾: Work with views instead of copies

📐 1D Array Indexing

Start with simple vector indexing.

import numpy as np

vector = np.array([10, 20, 30, 40, 50])
print(f"Vector: {vector}")

# Single element access
print(f"Element at index 2: {vector[2]}")
print(f"Last element: {vector[-1]}")

# Basic slicing
print(f"Slice [1:4]: {vector[1:4]}")
print(f"Every 2nd element: {vector[::2]}")

🎯 2D Array Basics

Work with matrices using row and column coordinates.

import numpy as np

matrix = np.array([[1, 2, 3], 
                   [4, 5, 6]])
print(f"Matrix: \n{matrix}")

# Element access
print(f"Element at [1, 2]: {matrix[1, 2]}")

# Row and column access
print(f"Row 0: {matrix[0]}")
print(f"Column 1: {matrix[:, 1]}")

🔪 Slicing Preview

Extract rectangular sections from arrays.

import numpy as np

data = np.array([[10, 20, 30, 40],
                 [50, 60, 70, 80],
                 [90, 100, 110, 120]])

print(f"Original: \n{data}")

# Row slicing
print(f"First 2 rows: \n{data[:2]}")

# Column slicing  
print(f"First 2 columns: \n{data[:, :2]}")

# Combined slicing
print(f"Center section: \n{data[1:3, 1:3]}")

🎭 Boolean Preview

Filter data using conditions.

import numpy as np

scores = np.array([85, 92, 78, 96, 89, 74])
names = np.array(['Alice', 'Bob', 'Carol', 'David', 'Eve', 'Frank'])

# Create boolean mask
high_scores = scores > 90
print(f"High scores mask: {high_scores}")
print(f"High performers: {names[high_scores]}")

📚 What You'll Learn

This section covers essential indexing techniques:

🎯 Real-World Examples

Student Grade Analysis

import numpy as np

# Grade data: Math, Science, English
grades = np.array([[85, 92, 78],  # Alice
                   [79, 85, 91],  # Bob
                   [94, 89, 96]])  # Carol

students = ['Alice', 'Bob', 'Carol']
subjects = ['Math', 'Science', 'English']

# Access specific data
print(f"Carol's Math grade: {grades[2, 0]}")
print(f"All Science grades: {grades[:, 1]}")

# Find high performers in Math
math_grades = grades[:, 0]
high_math = math_grades > 85
print(f"Strong in Math: {np.array(students)[high_math]}")

Sales Data Analysis

import numpy as np

# Daily sales for 4 stores
sales = np.array([[120, 135, 145, 160],  # Store A
                  [98, 112, 125, 140],   # Store B
                  [156, 167, 175, 185],  # Store C
                  [134, 145, 155, 170]]) # Store D

stores = ['Store A', 'Store B', 'Store C', 'Store D']

# Best performing day for each store
best_days = sales.argmax(axis=1)
print(f"Best day index per store: {best_days}")

# Top performing stores
avg_sales = sales.mean(axis=1)
top_stores = avg_sales > 150
print(f"Top stores: {np.array(stores)[top_stores]}")

🎯 Key Concepts

🚀 Ready to Start?

Master array indexing to unlock NumPy's full power! Start with the fundamentals and work your way up to advanced techniques.

Begin with: Basic Indexing and Slicing

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent