🐍 NumPy – Field Access
In NumPy, field access is used when working with structured arrays (also called record arrays).
Unlike normal NumPy arrays, structured arrays allow you to assign names to columns, similar to a database table or pandas DataFrame.
Field access allows you to:
- Access data using column names
- Work with structured datasets
- Store mixed data types
- Handle tabular data efficiently
What is Field Access in NumPy?
Field access means retrieving values from a structured array using named fields (column names) instead of numeric indices.
Example Concept
Name Age Score
------------------------
Aman 20 85
John 22 90
Sara 21 88You can access:
- Name column
- Age column
- Score column
🟢 Creating Structured Arrays
To use field access, we must define a data type with named fields.
Example
import numpy as np
data = np.array([
("Aman", 20, 85),
("John", 22, 90),
("Sara", 21, 88)
], dtype=[("name", "U10"), ("age", "i4"), ("score", "i4")])
print(data)Output:
[('Aman', 20, 85)
('John', 22, 90)
('Sara', 21, 88)]🔵 Accessing Fields (Column Access)
You can access data using field names.
Access Names Column
print(data["name"])Output:
['Aman' 'John' 'Sara']Access Age Column
print(data["age"])Output:
[20 22 21]Access Score Column
print(data["score"])Output:
[85 90 88]🟡 Accessing Individual Records
You can still use indexing for rows.
print(data[0])Output:
('Aman', 20, 85)Access Specific Field from Row
print(data[1]["score"])Output:
90🟣 Field Modification
You can also modify values using field names.
data["score"][0] = 95
print(data)Output:
[('Aman', 20, 95)
('John', 22, 90)
('Sara', 21, 88)]🔴 Filtering Using Fields
You can apply conditions directly on fields.
Example: Students with score > 85
result = data[data["score"] > 85]
print(result)Output:
[('John', 22, 90)
('Sara', 21, 88)]🟤 Multiple Field Access
You can select multiple fields together.
print(data[["name", "score"]])Output:
[('Aman', 95)
('John', 90)
('Sara', 88)]🧠 Why Use Field Access?
Field access is useful because:
- It makes data more readable
- Works like database tables
- Supports mixed data types
- Useful for structured datasets
⚡ Real-World Use Cases
1. Student Records
- name
- age
- marks
2. Employee Data
- id
- salary
- department
3. Sensor Data
- temperature
- humidity
- timestamp
📊 Field Access vs Normal Indexing
| Feature | Normal Array | Structured Array |
|---|---|---|
| Access method | Index (0,1,2) | Field names |
| Data type | Single type | Multiple types |
| Readability | Low | High |
| Use case | Numerical data | Tabular data |
🚀 Performance Note
- Structured arrays are slightly slower than regular arrays
- But they provide better readability and organization
- Ideal for small to medium structured datasets
⚠️ Common Mistakes
1. Using field name incorrectly
KeyError: 'score'✔ Fix: Ensure field name matches dtype definition
2. Using normal arrays instead of structured arrays
✔ Field access works only with structured dtype arrays
🧾 Summary
NumPy field access allows you to:
- Access data using column names
- Work with structured arrays
- Filter and modify specific fields
- Handle tabular-like data efficiently
🏁 Conclusion
Field access in NumPy is a powerful feature for working with structured data. It brings a table-like approach to array handling, making it easier to manage real-world datasets such as employee records, student marks, and sensor data.
It is especially useful when you need clarity, structure, and named data access in numerical computing.


0 Comments