# Create a record array from a (flat) list of array and set a valid datatype for all in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To create a record array from a (flat) list of array, use the numpy.core.records.fromarrays() method in Python Numpy. The datatype is set using the "dtype" parameter.

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.8, 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("Array1 type...", arr1.dtype)
print("Array2 type...", arr2.dtype)
print("Array3 type...", arr3.dtype)

Get the dimensions of the Arrays −

print("Array1 Dimensions...", arr1.ndim)
print("Array2 Dimensions...", arr2.ndim)
print("Array3 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 datatype is set using the "dtype" parameter −

rec = np.core.records.fromarrays([arr1,arr2,arr3], dtype=np.dtype([('a', np.int32), ('b', np.float32),
('c','S3' )]))
print("Record Array...",rec)

## 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.8, 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("Array1 type...", arr1.dtype)
print("Array2 type...", arr2.dtype)
print("Array3 type...", arr3.dtype)

# Get the dimensions of the Arrays
print("Array1 Dimensions...", arr1.ndim)
print("Array2 Dimensions...", arr2.ndim)
print("Array3 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 datatype is set using the "dtype" parameter
rec = np.core.records.fromarrays([arr1,arr2,arr3],
dtype=np.dtype([('a', np.int32), ('b', np.float32), ('c','S3' )]))
print("Record Array...",rec)

## Output

Array1...
[[ 5 10 15]
[20 25 30]]
Array2...
[[ 9. 18.8 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. , b'12') (10, 18.8, b'bbb') (15, 24. , b'joh')]
[(20, 87.5, b'5.6') (25, 65. , b'29') (30, 23.8, b'k')]]


Updated on 17-Feb-2022 10:20:04