Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Manipulating Structured Arrays Explained – Edit, Update, Filter Data in Python

NumPy Manipulating Structured Arrays

Structured arrays in NumPy allow you to store mixed data types like:

  • Name (string)
  • Age (integer)
  • Salary (float)

But real-world data is not static — you often need to modify, update, filter, and sort it.

This is where manipulating structured arrays becomes important.


What is Structured Array Manipulation?

It means performing operations such as:

  • Updating values
  • Filtering data
  • Sorting records
  • Adding or modifying fields

Why Manipulate Structured Arrays?

  • Clean and update datasets
  • Extract useful information
  • Organize data efficiently
  • Prepare data for analysis or ML
  • Work like a lightweight database

1. Creating a Structured Array

import numpy as np

data = np.array([
("Alice", 25, 50000),
("Bob", 30, 60000),
("Charlie", 28, 55000)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "i4")])

print(data)

2. Accessing Fields

print(data["name"])
print(data["age"])

Output

['Alice' 'Bob' 'Charlie']
[25 30 28]

3. Updating Values in Structured Arrays

data["salary"][0] = 52000

print(data)

Output

[('Alice', 25, 52000)
('Bob', 30, 60000)
('Charlie', 28, 55000)]

4. Updating Multiple Fields

data["age"] = data["age"] + 1

print(data)

5. Filtering Structured Arrays

filtered = data[data["age"] > 27]

print(filtered)

Output

[('Bob', 30, 60000)
('Charlie', 28, 55000)]

6. Filtering by Salary

high_salary = data[data["salary"] > 55000]

print(high_salary)

7. Sorting Structured Arrays

sorted_data = np.sort(data, order="age")

print(sorted_data)

Output

[('Alice', 25, 52000)
('Charlie', 28, 55000)
('Bob', 30, 60000)]

8. Sorting by Salary

sorted_salary = np.sort(data, order="salary")

print(sorted_salary)

9. Selecting Specific Records

print(data[0])  # First record
print(data[-1]) # Last record

10. Modifying Strings in Structured Arrays

data["name"] = np.char.upper(data["name"])

print(data)

11. Adding New Values (Indirect Method)

Structured arrays are fixed-size, but we can use append:

new_data = np.append(data, [("David", 35, 70000)])

print(new_data)

12. Real-World Example: Employee Update System

import numpy as np

employees = np.array([
("John", 40, 80000),
("Emma", 29, 65000),
("Mike", 33, 72000)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "i4")])

employees["salary"][1] = 68000

print(employees)

13. Real-World Example: Student Records

students = np.array([
("A", 90),
("B", 75),
("C", 88)
], dtype=[("name", "U10"), ("marks", "i4")])

students["marks"] = students["marks"] + 5

print(students)

Common Manipulation Operations

OperationDescription
Update                 Change values
Filter                 Select data
Sort                 Arrange records
Access                 Read fields
Modify                 Transform data

Advantages of Manipulating Structured Arrays

  • Easy data cleaning
  • Fast operations
  • Column-based control
  • Useful in analytics
  • Works like database tables

Summary

Manipulating structured arrays in NumPy allows you to efficiently update, filter, sort, and manage complex datasets. This makes it highly useful for real-world data processing.

This functionality is part of NumPy and widely used in applications built with Python.


Conclusion

Structured array manipulation is essential for handling real-world data. It provides powerful tools for updating and organizing structured datasets in Python efficiently.




Post a Comment

0 Comments