3. Write a Python Numpy program to generate a structured array with the given details in the following output and sort the marks in increasing order.
The output is:
data_type = [('name', 'S20'), ('course', 'S20'), ('mark', float)]
students_marks = [('Mark', 'Python', 68.5), ('Nicole', 'Python', 86.5),('Iven', 'Machine Learning', 68.0), ('Alvin', 'Machine Learning', 80.5)]
Expected Answer:
Initial array:
[(b'Mark', b'Python', 68.5) (b'Nicole', b'Python', 86.5)
(b'Iven', b'Machine Learning', 68. ) (b'Alvin', b'Machine Learning', 80.5)]
Sort by marks:
[(b'Iven', b'Machine Learning', 68. ) (b'Alvin', b'Machine Learning', 80.5)
(b'Mark', b'Python', 68.5) (b'Nicole', b'Python', 86.5)]
Sample Answer:
import numpy as np data_type = [('name', 'S20'), ('course', 'S20'), ('mark', float)] students_marks = [('Mark', 'Python', 68.5), ('Nicole', 'Python', 86.5),('Iven', 'Machine Learning', 68.0), ('Alvin', 'Machine Learning', 80.5)] students = np.array(students_marks, dtype=data_type) print("Initial array:") print(students) print("\nSort by marks:") print(np.sort(students, order=['course','mark']))
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)