w3resource

Rename fields in NumPy Structured array

NumPy: Structured Arrays Exercise-13 with Solution

Renaming Fields:

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

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)

# Rename the 'height' field to 'stature'
new_dtype = [('name', 'U10'), ('age', 'i4'), ('stature', 'f4')]

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

# Copy data from the old array to the new array
for field in structured_array.dtype.names:
    renamed_array[field if field != 'height' else 'stature'] = structured_array[field]

# Print the new structured array with renamed fields
print("\nStructured Array with 'height' field renamed to 'stature':")
print(renamed_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 'height' field renamed to 'stature':
[('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)]

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'.
  • Rename Field:
    • Defined a new data type new_dtype with the 'height' field renamed to 'stature'.
    • Create a new empty structured array renamed_array with the new data type.
  • Copy Data:
    • Used a loop to copy data from the original array to the new array, renaming the 'height' field to 'stature' during the process.
  • Finally print the new structured array to verify that the 'height' field has been successfully renamed to 'stature'.

Python-Numpy Code Editor:

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

Previous: Convert Height field to regular array in NumPy Structured array.
Next: Load data from CSV file into 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://198.211.115.131/python-exercises/numpy/rename-fields-in-numpy-structured-array.php