w3resource

Merge fields into a new field in NumPy Structured array


NumPy: Structured Arrays Exercise-19 with Solution


Merging Fields into a 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).

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: ",structured_array)

# Define a new data type with an additional 'name_age' field
new_dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4'), ('name_age', 'U14')]

# Create a new structured array with the new data type
new_structured_array = np.empty(structured_array.shape, dtype=new_dtype)

# Copy the existing fields to the new structured array
for field in structured_array.dtype.names:
    new_structured_array[field] = structured_array[field]

# Create the 'name_age' field by concatenating 'name' and 'age' fields
new_structured_array['name_age'] = np.array([f"{name}_{age}" for name, age in zip(structured_array['name'], structured_array['age'])])

# Print the new structured array with the 'name_age' field
print("\nStructured Array with 'name_age' field:")
print(new_structured_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)]

Structured Array with 'name_age' field:
[('Lehi Piero', 25, 5.5, 'Lehi Piero_25')
 ('Albin Acha', 30, 5.8, 'Albin Acha_30')
 ('Zerach Hav', 35, 6.1, 'Zerach Hav_35')
 ('Edmund Ter', 40, 5.9, 'Edmund Ter_40')
 ('Laura Feli', 28, 5.7, 'Laura Feli_28')]

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 Data Type:
    • Defined a new data type 'new_dtype' with an additional 'name_age' field, which is a string of up to 14 characters.
  • Create New Structured Array:
    • Create a new empty structured array new_structured_array with the new data type.
  • Copy Existing Fields:
    • Copy the existing fields ('name', 'age', 'height') from the original structured array to the new structured array.
  • Create 'Name_Age' Field:
    • Created the 'name_age' field by concatenating the 'name' and 'age' fields for each record, using a list comprehension and the zip function.
  • Print the new structured array to verify the addition of the 'name_age' field.

Python-Numpy Code Editor: