Split NumPy Structured array based on condition
NumPy: Structured Arrays Exercise-11 with Solution
Splitting Structured Arrays:
Write a NumPy program to split a structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float) based on a condition (e.g., age > 25).
Sample Solution:
Python Code:
import numpy as np
# Define the data type for the structured array
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
# Create the structured array with sample data
structured_array = np.array([
('Lehi Piero', 25, 5.5),
('Albin Achan', 30, 5.8),
('Zerach Hava', 35, 6.1),
('Edmund Tereza', 40, 5.9),
('Laura Felinus', 28, 5.7)
], dtype=dtype)
print("Original Structured Array:")
print(structured_array)
# Split the array based on the condition age > 25
condition = structured_array['age'] > 25
array_above_25 = structured_array[condition]
array_25_and_below = structured_array[~condition]
# Print the resulting arrays
print("\nArray with age > 25:")
print(array_above_25)
print("\nArray with age <= 25:")
print(array_25_and_below)
Output:
Original Structured Array: [('Lehi Piero', 25, 5.5) ('Albin Acha', 30, 5.8) ('Zerach Hav', 35, 6.1) ('Edmund Ter', 40, 5.9) ('Laura Feli', 28, 5.7)] Array with age > 25: [('Albin Acha', 30, 5.8) ('Zerach Hav', 35, 6.1) ('Edmund Ter', 40, 5.9) ('Laura Feli', 28, 5.7)] Array with age <= 25: [('Lehi Piero', 25, 5.5)]
Explanation:
- Import Libraries:
- Imported numpy as "np" for array creation and manipulation.
- Define Data Type:
- Define the data type for the structured array using a list of tuples. Each tuple specifies a field name and its corresponding data type. The data types are:
- 'U10' for a string of up to 10 characters.
- 'i4' for a 4-byte integer.
- 'f4' for a 4-byte float.
- Create a Structured Array:
- Created the structured array using np.array(), providing sample data for five individuals. Each individual is represented as a tuple with values for 'name', 'age', and 'height'.
- Define the condition:
- Define a condition to split the array based on age. The condition checks if 'age' is greater than 25.
- Split Array:
- Split the array into two based on the condition:
- array_above_25: contains records where age > 25.
- array_25_and_below: contains records where age <= 25.
- Print the Resulting Arrays:
- Print the resulting arrays to verify the split.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Combine two Structured arrays in NumPy.
Next: Convert Height field to regular array in NumPy Structured array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/numpy/split-numpy-structured-array-based-on-condition.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics