w3resource

Add new record to NumPy Structured array

NumPy: Structured Arrays Exercise-4 with Solution

Adding New Records:

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.

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)

# Define the new record to be added
new_record = np.array([('Nela Suna', 25, 5.9)], dtype=dtype)

# Add the new record to the structured array
# Use np.append to concatenate the new record to the existing array
updated_array = np.append(structured_array, new_record)

# Print the updated structured array
print("\nUpdated Structured Array:")
print(updated_array)

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

Updated 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) ('Nela Suna', 25, 5.9)]

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 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 New Record:
    • Define the new record to be added as a NumPy array with the same data type as the structured array. The new record has the values 'name': 'Arlo Arias', 'age': 25, 'height': 5.9.
  • Add New Record:
    • Used np.append to concatenate the new record to the existing structured array, resulting in an updated array.
  • Print Updated Structured Array:
    • Print the updated structured array to verify the addition of the new record.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Update field value in NumPy Structured array.
Next: Filter records by age in NumPy Structured array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://www.w3resource.com/python-exercises/numpy/add-new-record-to-numpy-structured-array.php