What are structured arrays in Python NumPy?

It is a heterogeneous data type that is similar to a database row. Steps that you must follow to create structured array are shown below-

  1. Call dtype() constructor and pass list having tuples in it. Each tuple must have field name and their data type. Eg-
    t = np.dtype([('productname', 'U30'), 
    			('numberofquantity', 'uint8'), 
    			('price', 'float32'), 
    			('inStock', 'bool')])
    You can also specify the field name and their data type in this way-
    t = np.dtype({
      'names':('productname', 'numberofquantity', 'price', 'inStock'),
      'formats':('U30', 'uint8', 'float32', 'bool')})
    In both of the above code, productname is represented by a 30-character string, numberofquantity is represented by a 8-bit unsigned integer, price is represented by 32-bit floating point number, and inStock is represented by boolean data type.
  2. Call array() function and pass a list having tuples in it. Each tuple must represent a record. And make dtype parameter equal to the one created above. Eg-
    arr = np.array([('Samsung Galaxy S2', 23, 15000, True), 
    				('Apple iPhone X', 56, 80000, True), 
    				('Motorola M20', 31, 11500, False)], dtype=t)

Example: Numpy employee structured array

import numpy as np

t = np.dtype([('empname', 'U40'), 
              ('age', 'u1'), 
              ('salary', 'u4'), 
              ('designation', 'U30')])

arr = np.array([('Mohit Natani', 28, 70000, 'Python Developer'), 
                ('Kanchan Sharma', 25, 60000, 'Digital Marketer'), 
                ('Radhika Rathore', 26, 52500, 'SEO Manager')], dtype=t)

#Get third row data
print(arr[2])
#Get designation of Mohit Natani
print(arr[0]['designation'])

Output of the above code

('Radhika Rathore', 26, 52500, 'SEO Manager')
Python Developer
Numpy employee structured array

How to access elements in Numpy structured array?

You need to specify two things to access an element-

  1. Index position
  2. Field name- It is optional and if not provided then you will get the specified indexed row data.
import numpy as np

t = np.dtype([('productname', 'U30'), 
              ('numberofquantity', 'uint8'), 
              ('price', 'float32'), 
              ('inStock', 'bool')])

arr = np.array([('Samsung Galaxy S2', 23, 15000, True), 
                ('Apple iPhone X', 56, 80000, True), 
                ('Motorola M20', 31, 11500, False)], dtype=t)

#Get the name of mobile from the first row
print(arr[0]['productname'])
#Get second row data
print(arr[1])
#Get all product names
print(arr['productname'])
#Get the name of mobile from the second row
print(arr[1][0])

Output of the above program

Samsung Galaxy S2
('Apple iPhone X', 56, 80000., True)
['Samsung Galaxy S2' 'Apple iPhone X' 'Motorola M20']
Apple iPhone X
Access elements in Numpy structured array

arr[0]['productname'] will return then mobile name from the first row- Samsung Galaxy S2

arr[1] will return the second row- ('Apple iPhone X', 56, 80000., True)

arr['productname'] will return a complete list of product name- ['Samsung Galaxy S2' 'Apple iPhone X' 'Motorola M20']

arr[1][0] will provide the mobile name from the second row- Apple iPhone X

Recommended Posts