Structured Numpy Array and how to create and sort it in Python
In this article we will learn what is structured numpy array, how to create it and how to sort with different functions.
What is a Structured Numpy Array ?
A Structured Numpy array is an array of structures where we can also make of homogeneous structures too.
Creating a Structured Numpy Array
To create structured numpy array we will pass list of tuples with elements in dtype parameter and we will create numpy array based on this stype.
import numpy as sc # Creation of structure dtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)] # Creation a Strucured Numpy array structured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype) print(structured_arr.dtype)
Output : [('Name', '<U10'), ('CGPA', '<f8'), ('Age', '<i4')]
Sort the Structured Numpy array by field ‘Name’ of the structure
How to Sort a Structured Numpy Array ?
We can sort a big structured numpy array by providing a parameter ‘order’ parameter provided by numpy.sort()
and numpy.ndarray.sort()
. Let’s sort the structured numpy array on the basis of field ‘Name
‘.
import numpy as sc # Creation of structure dtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)] # Creation a Strucured Numpy array structured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype) sor_arr = sc.sort(structured_arr, order='Name') print('Sorted Array on the basis on name : ') print(sor_arr)
Output : Sorted Array on the basis on name : [('Ben', 8.8, 18) ('Rani', 9.4, 15) ('Saswat', 7.6, 16) ('Tanmay', 9.8, 17)]
Sort the Structured Numpy array by field ‘Age’ of the structure
We can also sort the structured numpy array on the basis of field ‘Marks
‘.
import numpy as sc # Creation of structure dtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)] # Creation a Strucured Numpy array structured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype) sor_arr = sc.sort(structured_arr, order='Age') print('Sorted Array on the basis on Age : ') print(sor_arr)
Output : Sorted Array on the basis on Age : [('Rani', 9.4, 15) ('Saswat', 7.6, 16) ('Tanmay', 9.8, 17) ('Ben', 8.8, 18)]
Sort the Structured Numpy array by ‘Name’ & ‘Age’ fields of the structure :
We can also sort Structured Numpy array based on multiple fields ‘Name
‘ & ‘Age
‘.
import numpy as sc # Creation of structure dtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)] # Creation a Strucured Numpy array structured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype) sor_arr = sc.sort(structured_arr, order=['Name','Age']) print('Sorted Array on the basis on name & age : ') print(sor_arr)
Output : Sorted Array on the basis on name & age: [('Ben', 8.8, 18) ('Rani', 9.4, 15) ('Saswat', 7.6, 16) ('Tanmay', 9.8, 17)]