w3resource

NumPy: Create a record array from a given regular array

NumPy: Array Object Exercise-190 with Solution

Write a NumPy program to create a record array from a given regular array.

Sample Solution:

Python Code:

import numpy as np
arra1 = np.array([("Yasemin Rayner", 88.5, 90),
                 ("Ayaana Mcnamara", 87, 99),
             ("Jody Preece", 85.5, 91)])
print("Original arrays:")
print(arra1)
print("\nRecord array;")
result = np.core.records.fromarrays(arra1.T,
                              names='col1, col2, col3',
                              formats = 'S80, f8, i8')
print(result)

Sample Output:

Original arrays:
[['Yasemin Rayner' '88.5' '90']
 ['Ayaana Mcnamara' '87' '99']
 ['Jody Preece' '85.5' '91']]

Record array;
[(b'Yasemin Rayner', 88.5, 90) (b'Ayaana Mcnamara', 87. , 99)
 (b'Jody Preece', 85.5, 91)]

Python Code Editor:

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

Previous:Write a NumPy program to find rows of a given array of shape (8,3) that contain elements of each row of another given array of shape (2,2).
Next: Write a NumPy program to get the block-sum (block size is 5x5) from a given array of shape 25x25.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}