NumPy Structured Arrays: Exercises, Practice and Solutions

NumPy Structured Arrays [ 20 exercises with solution]

The following exercises will help you become familiar with NumPy structured arrays, including creating, modifying, sorting, and filtering heterogeneous data.

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

1. 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. 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. 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. 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. 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. 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. 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. 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. 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. Write a NumPy program that combines two structured arrays with the same dtype into one.
Click me to see the sample solution

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

﻿