w3resource

NumPy Structured Arrays: Exercises, Practice and Solutions


This resource offers a total of 100 NumPy Structured Arrays problems for practice. It includes 20 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

The following exercises explore NumPy structured arrays, focusing on creation, modification, filtering, sorting, and advanced indexing. They help in managing heterogeneous data efficiently, making structured arrays a powerful tool for handling tabular datasets within NumPy.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Structured Array Creation

Write a NumPy program that creates a structured array with fields for 'name' (string), 'age' (integer), and 'height' (float). Populate it with sample data for five individuals.

Click me to see the sample solution

2. Access 'Name' Field

Write a NumPy program that accesses and prints all values of the 'Name' field from the structured array created in exercise.

Click me to see the sample solution

3. Update Age Field

Write a NumPy program that updates the 'age' field for the first individual in the structured array created in exercise 1 to 30.

Click me to see the sample solution

4. Add New Record

Write a NumPy program that adds a new record to the structured array created array with fields for 'name' (string), 'age' (integer), and 'height' (float) with the fields: 'name': ' Nela Suna ', 'age': 25, 'height': 5.9.

Click me to see the sample solution

5. Filter Records by Age

Write a NumPy program to filter and print all records where the 'age' field is greater than 25 from the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution

6. Sort by Height

Write a NumPy program that sorts the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float), by the 'height' field in ascending order and prints the sorted array.

Click me to see the sample solution

7. Average Age Calculation

Write a NumPy program to calculate and print the average age of individuals in the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution

8. Delete Record by Name

Write a NumPy program to delete the record where 'name' is a specific name from the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution

9. Increment Age Field

Write a NumPy program to increase the 'age' field by 1 for all individuals in the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution

10. Combine Structured Arrays

Write a NumPy program that combines two structured arrays with the same dtype into one.

Click me to see the sample solution

11. Split Array Based on Condition

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).

Click me to see the sample solution

12. Convert Height Field to Regular Array

Write a NumPy program to convert the 'height' field of the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float) to a regular NumPy array.

Click me to see the sample solution

13. Rename Height Field to Stature

Write a NumPy program to rename the 'height' field to 'stature' in the structured array with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution

14. Load Structured Array from CSV

Write a NumPy program that loads data from a CSV file into a structured array. Assume the CSV file has columns corresponding to 'name', 'age', and 'height'.

Click me to see the sample solution

15. Save Structured Array to File

Write a NumPy program to save a structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float) to a file using NumPy's save function.

Click me to see the sample solution

16. Iterate Over Records and Print

Write a NumPy program that iterates over each record in the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float) and prints the 'name' and 'height' fields.

Click me to see the sample solution

17. Structured Array with Nested Fields

Write a NumPy program that creates a structured array with nested fields, such as 'person' (which has sub-fields 'name' and 'age') and 'score' (integer).

Click me to see the sample solution

18. Advanced Indexing on Structured Array

Write a NumPy program that uses advanced indexing to select records from the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float) where both 'age' is greater than 25 and 'height' is less than 6.0.

Click me to see the sample solution

19. Concatenate 'name' and 'age' to New Field

Write a NumPy program that creates a new field 'name_age' that concatenates the 'name' and 'age' fields from the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution

20. Broadcast Value across 'Height' Field

Write a NumPy program to broadcast a value (e.g., set all 'height' values to 6.0) across the 'height' field in the structured array created with fields for 'name' (string), 'age' (integer), and 'height' (float).

Click me to see the sample solution


Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.