Header Ads Widget

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

NumPy Record Arrays Explained – Easy Structured Data Handling in Python

NumPy Record Arrays

Record arrays in NumPy are an extension of structured arrays that make data access easier and more readable.

Instead of using bracket notation like:

data["name"]

You can use dot notation:

data.name

This makes code cleaner and more intuitive.


What are Record Arrays?

Record arrays are structured arrays with attribute-style access to fields.

They allow you to treat structured data like objects.


Why Use Record Arrays?

  • Cleaner syntax
  • Easier field access
  • More readable code
  • Works like objects
  • Useful in data analysis

1. Creating a Record Array

We use np.rec.array() or view().

import numpy as np

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

print(data)

2. Accessing Fields (Dot Notation)

print(data.name)
print(data.age)

Output

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

3. Accessing Single Record

print(data[0])

Output

('Alice', 25, 50000)

4. Creating Record Array from Structured Array

import numpy as np

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

record = structured.view(np.recarray)

print(record.name)

5. Updating Values in Record Arrays

data.salary[0] = 52000

print(data)

6. Filtering Record Arrays

filtered = data[data.age > 27]

print(filtered)

7. Sorting Record Arrays

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

print(sorted_data)

8. Modifying String Fields

data.name = np.char.upper(data.name)

print(data.name)

9. Real-World Example: Employee Records

import numpy as np

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

print(employees.salary)

10. Real-World Example: Student Data

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

print(students.marks)

Record Array vs Structured Array

FeatureStructured ArrayRecord Array
Access style                data["name"]                      data.name
Readability                Medium                      High
Performance                Fast                      Similar
Use case                Data processing                      Clean coding

Advantages of Record Arrays

  • Easy dot notation access
  • Cleaner code
  • Better readability
  • Good for object-like data
  • Useful in data science workflows

Summary

NumPy record arrays provide a more user-friendly way to access structured data using dot notation. They simplify code and make structured data handling more intuitive.

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


Conclusion

Record arrays are a powerful enhancement over structured arrays, making data access simpler and more readable. They are especially useful in data science and analytics projects.




Post a Comment

0 Comments