Create a record array from a (flat) list of array and fetch specific values based on index in Numpy

To create a record array from a (flat) list of array, use the numpy.core.records.fromarrays() method in Python Numpy. The names is set using the "names" parameter. The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3']. An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used.

It returns the record array consisting of given arrayList columns. The first parameter is a List of arraylike objects (such as lists, tuples, and ndarrays). The dtype is the valid dtype for all arrays. The formats, names, titles, aligned, byteorder parameters, if dtype is None, these arguments are passed to numpy.format_parser to construct a dtype.

Steps

At first, import the required library −

import numpy as np

Create a new array using the numpy.array() method −

arr1 = np.array([[5, 10, 15], [20, 25, 30]])
arr2 = np.array([[9, 18, 24], [87.5, 65, 23.8]])
arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])

Display the arrays −

print("Array1...
",arr1) print("Array2...
",arr2) print("Array3...
",arr3)

Get the type of the arrays −

print("\nArray1 type...
", arr1.dtype) print("\nArray2 type...
", arr2.dtype) print("\nArray3 type...
", arr3.dtype)

Get the dimensions of the Arrays −

print("\nArray1 Dimensions...
", arr1.ndim) print("\nArray2 Dimensions...
", arr2.ndim) print("\nArray3 Dimensions...
", arr3.ndim)

To create a record array from a (flat) list of array, use the numpy.core.records.fromarrays() method −

rec = np.core.records.fromarrays([arr1,arr2,arr3], names = 'a,b,c')
print("\nRecord Array...
",rec)

Let us try to fetch values −

print("\nFetching the values...
",rec[0]) print("\nFetching the values...
",rec[1])

Example

import numpy as np

# Create a new array using the numpy.array() method
arr1 = np.array([[5, 10, 15], [20, 25, 30]])
arr2 = np.array([[9, 18, 24], [87.5, 65, 23.8]])
arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])

# Display the arrays
print("Array1...
",arr1) print("Array2...
",arr2) print("Array3...
",arr3) # Get the type of the arrays print("\nArray1 type...
", arr1.dtype) print("\nArray2 type...
", arr2.dtype) print("\nArray3 type...
", arr3.dtype) # Get the dimensions of the Arrays print("\nArray1 Dimensions...
", arr1.ndim) print("\nArray2 Dimensions...
", arr2.ndim) print("\nArray3 Dimensions...
", arr3.ndim) # To create a record array from a (flat) list of array, use the numpy.core.records.fromarrays() method in Python Numpy # The names is set using the "names" parameter # The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3']. # An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used. rec = np.core.records.fromarrays([arr1,arr2,arr3], names = 'a,b,c') print("\nRecord Array...
",rec) print("\nFetching the values...
",rec[0]) print("\nFetching the values...
",rec[1])

Output

Array1...
[[ 5 10 15]
[20 25 30]]
Array2...
[[ 9. 18. 24. ]
[87.5 65. 23.8]]
Array3...
[['12' 'bbb' 'john']
['5.6' '29' 'k']]

Array1 type...
int64

Array2 type...
float64

Array3 type...
<U4

Array1 Dimensions...
2

Array2 Dimensions...
2

Array3 Dimensions...
2

Record Array...
[[( 5, 9. , '12') (10, 18. , 'bbb') (15, 24. , 'john')]
[(20, 87.5, '5.6') (25, 65. , '29') (30, 23.8, 'k')]]

Fhing the values...
[( 5, 9., '12') (10, 18., 'bbb') (15, 24., 'john')]

Fhing the values...
[(20, 87.5, '5.6') (25, 65. , '29') (30, 23.8, 'k')]

Updated on: 2022-02-17T10:34:19+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements