Q:

Write a NumPy program to extract all the rows to compute the student weight from a given array (student information) where a specific column starts with a given character

0

Write a NumPy program to extract all the rows to compute the student weight from a given array (student information) where a specific column starts with a given character.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
np.set_printoptions(linewidth=100)
student =  np.array([['01', 'V', 'Debby Pramod', 30.21],
 ['02', 'V', 'Artemiy Ellie', 29.32],
 ['03', 'V', 'Baptist Kamal', 31.00],
 ['04', 'V', 'Lavanya Davide', 30.22],
 ['05', 'V', 'Fulton Antwan', 30.21],
 ['06', 'V', 'Euanthe Sandeep', 31.00],
 ['07', 'V', 'Endzela Sanda', 32.00],
 ['08', 'V', 'Victoire Waman', 29.21],
 ['09', 'V', 'Briar Nur', 30.00],
 ['10', 'V', 'Rose Lykos', 32.00]])
print("Original array:")
print(student)
char='E'
result = student[np.char.startswith(student[:,2], char)]
print("\nTotal weight, where student name starting with",char)
print(np.round(result[:, 3].astype(float).sum(), 2))
char='D'
result = student[np.char.startswith(student[:,2], char)]
print("\nTotal weight, where student name starting with",char)
print(np.round(result[:, 3].astype(float).sum(), 2))

Sample Output:

Original array:
[['01' 'V' 'Debby Pramod' '30.21']
 ['02' 'V' 'Artemiy Ellie' '29.32']
 ['03' 'V' 'Baptist Kamal' '31.0']
 ['04' 'V' 'Lavanya Davide' '30.22']
 ['05' 'V' 'Fulton Antwan' '30.21']
 ['06' 'V' 'Euanthe Sandeep' '31.0']
 ['07' 'V' 'Endzela Sanda' '32.0']
 ['08' 'V' 'Victoire Waman' '29.21']
 ['09' 'V' 'Briar Nur' '30.0']
 ['10' 'V' 'Rose Lykos' '32.0']]

Total weight, where student name starting with E
63.0

Total weight, where student name starting with D
30.21

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now