6 Ways to store different datatype in one NumPy array

Numpy

In this post, we are going to learn 6 Ways to store different datatype in one NumPy array by using np. concatenate(), np. append(),fromarrays(),np.array(), merge_arrays(). Can we store different data types that we will understand in this post?

1. Using np.concatenate() to store different datatype NumPy arrays


In this Python program example, We have created a numpy array that contains an element of the different datatype by specifying datatype as an object because all objects come under object data type in python. Secondly, after using the numpy concatenate() function, it two stores both arrays into one numpy array.

import numpy as np



nparr1 = np.array([3, 6, 9,'x','y'],dtype='O')


nparr2 = np.array(['app', 'data', 'info'])


resarr = np.concatenate([nparr1, nparr2])

print(resarr)
print(resarr.dtype)

Output

[3 6 9 'x' 'y' 'app' 'data' 'info']
object

2. fromarrays() to store different datatype in one NumPy array


In this Python program example, we will discuss how to store different datatype in one NumPy array, we have two numpy arrays of the different datatype, by using the numpy fromarrays() function we are converting both arrays into one array. We can access attributes of the record as ‘keys’ and ‘values’ we have accessed.

import numpy as np


npkeyarr = np.array(['app', 'soft', 'data', 'info', 'num'])
npValarr = np.array([14,15,3,6,9])
nprecords = np.rec.fromarrays((npkeyarr, npValarr), names=('keys', 'Values'))


print('NumPy array:',nprecords)

print('\nkeys in NumPy array:',nprecords['keys'])

print('\nvalues in NumPy array:\n',nprecords['Values'])

Output

NumPy array: [('app', 14) ('soft', 15) ('data',  3) ('info',  6) ('num',  9)]

keys in NumPy array: ['app' 'soft' 'data' 'info' 'num']

values in NumPy array:
 [14 15  3  6  9]

3. np.array() to store different datatype in one NumPy array


In this Python program, we have used a standard built function np. array() function. By passing the different datatype as list of tuple dtype=([(‘keys’, ‘|S3’), (‘Values’, ‘i8’)]).where ‘S3’ is the length of string we can change as per need.

import numpy as np


nprecords  = np.array([('app', 14) ,('soft', 15), ('data',  3) ,('info',  6) ,('num',  9)], 
                      dtype=([('keys', '|S3'), ('Values', 'i8')]))

print('NumPy array:',nprecords )

#mutiple datatype array

nparr1 = np.array([3, 6, 9,{'a':10,'b':20},[2,3]],dtype='O')


print(nparr1)

print(nparr1.dtype)

Output

NumPy array: [(b'app', 14) (b'sof', 15) (b'dat',  3) (b'inf',  6) (b'num',  9)]

[3 6 9 {'a': 10, 'b': 20} list([2, 3])]
object

4. merge_arrays() to store different datatype in one NumPy array


In this Python program, we have used numpy.lib library merge_arrays to store two different datatypes array int, a string that is combined into one numpy array.

import numpy as np
from numpy.lib import recfunctions as rfn

nparr1 = np.array([3, 6, 9])
nparr2 = np.array(['app', 'data', 'info'])
resArr = rfn.merge_arrays((nparr1, nparr2))

print(resArr)

print(resArr.dtype)

Output

[(3, 'app') (6, 'data') (9, 'info')]
[('f0', '<i4'), ('f1', '<U4')]

5. hstack() to store different datatype in one NumPy array


In this Python program example, we have created two arrays of datatype objects and we are storing different numpy array types using hstack() function that stacks the arrays in sequence column-wise. To stack arrays row-wise we can use the stack() numpy function.

  • First import numpy library using “import numpy as np”
  • Convert arrays to object datatype
  • hstack() function that stacks the arrays in sequence column-wise
import numpy as np


nparr1 = np.array([3, 6, 9],dtype='O')


nparr2 = np.array(['app', 'data', 'info'],dtype='U4')

data = np.hstack([nparr1, nparr2])



print(data)

print(data.dtype)

Output

[3 6 9 'app' 'data' 'info']
object

6. using np.append() to store different datype NumPy arrays


In this Python program, we will learn how to store different datatype in one NumPy array.

  • Import NumPy library using import numpy as np
  • Convert array to array to object datatype.
  • Append two arrays to one numpy array using np.append()
import numpy as np



nparr1 = np.array([3, 6, 9],dtype='O')


nparr2 = np.array(['app', 'data', 'info'],dtype='U4')

resarr = np.append(nparr1, nparr2)
 

print(resarr)

Output

[3 6 9 {'a': 10, 'b': 20} list([2, 3])]
object

FAQ

Can array store different data types

Yes, numpy array can store different datatype String, Integer, Complex, Float, Boolean.