Store different datatypes in one Numpy Array

In NumPy module, well structured arrays provide an effective way to handle heterogeneous data, where each element has a different type. While regular arrays with numpy store the items of a single data type eg. integers or floats etc. structured arrays allow you to define a mix of data types within one array. This is particularly useful when you need to represent more complex data, such as the attributes of a person, where each person has a name , age, and height which belongs to string , integer and float datatype and many other fields respectively.

To create a structured array, we use np.dtype()  to define a custom data type for each field in the array. For example, we might use ‘U15’ to specify a Unicode string of up to 15 characters for the name, ‘i4′ for a 32-bit integer to represent age, and ‘f4 for a 32-bit floatingpoint number for height, we can use more such different data types as per the fields in our array. This ensures that each field in the array has the appropriate type.One of the key advantages of using structured arrays is the ability to easily access specific fields using their names. You can access the name,age,height fields (mentioned in the code) individually, which makes it easier to work with complex data. Structured arrays offer a more memory-efficient and organized way to store and manipulate data compared to using general object arrays.

Storing different datatype elements in one NumPy array,a practical approach

 

To begin with we first import the NumPy library, which is essential for working with arrays and other numerical computaions.

import numpy as np

In the next step we create a NumPy array called mixed_arr with elements of different datatypes.The dtype=object ensures that the array can store elements of different datatypes and also help to display the type of the field,a for loop iterates over each element in mixed_arr, and the type(i) function is used to print the data type of each element.

mixed_arr = np.array([1, 'hello', 3.14, True], dtype=object)
print("Different data types in one numpy array would look like this:-")
print(mixed_arr)
print("The datatype of the items in the above mentioned numpy array are:- ")
for i in mixed_arr:
    print(i," dataype is= ", type(i))

Now we will define a Structured data type ‘person_type’ for ‘name” unicode value will be U15 which means maximum length of 15 characters,for ‘age’ unicode value will be i4(4 byte integer) and for ‘height’ it will be f4 (4 byte floating point number).Next we create a person array using a defined person_type then we print the structured array to show the combination of different data types stored for each person.

person_type = np.dtype([('name', 'U15'), ('age', 'i4'), ('height', 'f4')])
person = np.array([('Rohit Sharma', 38, 5.8), ('Virat Kohli', 37, 5.7)], dtype=person_type)
print("Structured Array with demonstrating different data types:")
print(person)
#accessing individual fields
print("Accessing Fields from the person array which is structured in nature:")
print("Names:", person['name'])
print("Ages:", person['age'])
print("Heights:", person['height'])


Output:

Different data types in one numpy array would look like this:-
[1 'hello' 3.14 True]
The datatype of the items in the above mentioned numpy array are:- 
1 dataype is= <class 'int'>
hello dataype is= <class 'str'>
3.14 dataype is= <class 'float'>
True dataype is= <class 'bool'>


Structured Array with demonstrating different data types:
[('Rohit Sharma', 38, 5.8) ('Virat Kohli', 37, 5.7)]
Accessing Fields from the person array which is structured in nature:
Names: ['Rohit Sharma' 'Virat Kohli']
Ages: [38 37]
Heights: [5.8 5.7]

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top