{"id":7279,"date":"2023-11-03T08:12:58","date_gmt":"2023-11-03T02:42:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=7279"},"modified":"2023-11-10T12:15:26","modified_gmt":"2023-11-10T06:45:26","slug":"what-is-a-structured-numpy-array-and-how-to-create-and-sort-it-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/what-is-a-structured-numpy-array-and-how-to-create-and-sort-it-in-python\/","title":{"rendered":"What is a Structured Numpy Array and how to create and sort it in Python?"},"content":{"rendered":"
In this article we will learn what is structured numpy array, how to create it and how to sort with different functions.<\/p>\n
A Structured Numpy array is an array of structures where we can also make of homogeneous structures too.<\/p>\n
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.<\/p>\n
import numpy as sc\r\n\r\n# Creation of structure\r\n\r\ndtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)]\r\n\r\n# Creation a Strucured Numpy array\r\n\r\nstructured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype)\r\n\r\nprint(structured_arr.dtype)<\/pre>\nOutput :\r\n[('Name', '<U10'), ('CGPA', '<f8'), ('Age', '<i4')]<\/pre>\nSort the Structured Numpy array by field \u2018Name’ of the structure<\/h3>\n
<\/a>How to Sort a Structured Numpy Array ?<\/h3>\n
We can sort a big structured numpy array by providing a parameter ‘order’<\/em> parameter provided by
numpy.sort()<\/code> and
numpy.ndarray.sort()<\/code>. Let’s sort the structured numpy array on the basis of field ‘
Name<\/code>‘.<\/p>\n
import numpy as sc\r\n\r\n# Creation of structure\r\n\r\ndtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)]\r\n\r\n# Creation a Strucured Numpy array\r\n\r\nstructured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype)\r\n\r\nsor_arr = sc.sort(structured_arr, order='Name')\r\n\r\nprint('Sorted Array on the basis on name : ')\r\n\r\nprint(sor_arr)<\/pre>\nOutput :\r\nSorted Array on the basis on name :\r\n[('Ben', 8.8, 18) ('Rani', 9.4, 15) ('Saswat', 7.6, 16)\r\n('Tanmay', 9.8, 17)]<\/pre>\nSort the Structured Numpy array by field \u2018Age’ of the structure<\/h3>\n
We can also sort the structured numpy array on the basis of field ‘
Marks<\/code>‘.<\/p>\n
import numpy as sc\r\n\r\n# Creation of structure\r\n\r\ndtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)]\r\n\r\n# Creation a Strucured Numpy array\r\n\r\nstructured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype)\r\n\r\nsor_arr = sc.sort(structured_arr, order='Age')\r\n\r\nprint('Sorted Array on the basis on Age : ')\r\n\r\nprint(sor_arr)<\/pre>\nOutput :\r\nSorted Array on the basis on Age :\r\n[('Rani', 9.4, 15) ('Saswat', 7.6, 16) ('Tanmay', 9.8, 17)\r\n('Ben', 8.8, 18)]<\/pre>\nSort the Structured Numpy array by \u2018Name\u2019 & \u2018Age\u2019 fields of the structure :<\/h3>\n
We can also sort Structured Numpy array based on multiple fields ‘
Name<\/code>‘ & ‘
Age<\/code>‘.<\/p>\n
import numpy as sc\r\n\r\n# Creation of structure\r\n\r\ndtype = [('Name', (sc.str_, 10)), ('CGPA', sc.float64), ('Age', sc.int32)]\r\n\r\n# Creation a Strucured Numpy array\r\n\r\nstructured_arr = sc.array([('Ben',8.8 , 18), ('Rani', 9.4, 15), ('Tanmay', 9.8, 17), ('Saswat', 7.6, 16)], dtype=dtype)\r\n\r\nsor_arr = sc.sort(structured_arr, order=['Name','Age'])\r\n\r\nprint('Sorted Array on the basis on name & age : ')\r\n\r\nprint(sor_arr)<\/pre>\nOutput :\r\nSorted Array on the basis on name & age:\r\n[('Ben', 8.8, 18) ('Rani', 9.4, 15) ('Saswat', 7.6, 16)\r\n('Tanmay', 9.8, 17)]<\/pre>\n","protected":false},"excerpt":{"rendered":"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. Creating a Structured Numpy Array How to Sort a Structured Numpy Array ? What is a Structured Numpy Array ? A …<\/p>\n