Python Forum
How can I add certain elements in this 2d data structure and calculate a mean
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I add certain elements in this 2d data structure and calculate a mean
#1
# ------------------------------------------------------------
# Import libraries
# ------------------------------------------------------------
# =====> Write your code here
mean = 0
index = 0
# ------------------------------------------------------------
# Constants
# ------------------------------------------------------------
# =====> Write your code here
Pass = (mean >= 50)
Fail = (mean <= 49)
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
myClass = [["Lang", "Carla", 49, 71, 95, 50],
           ["Bucklund", "Pia", 86, 78, 83, 64],
           ["Lang", "Jason", 95, 57, 92, 62],
           ["Dimitrousis", "Hector", 93, 45, 89, 96],
           ["Owens", "Sunna", 45, 50, 46, 54],
           ["Goldin", "Sandra", 25, 60, 45, 55],
           ["Giles", "Seth", 67, 73, 93, 64],
           ["Gurillo", "Melanie", 88, 88, 62, 79],
           ["Rykiel", "Kari", 67, 92, 54, 86],
           ["Shailes", "Dennis", 56, 70, 84, 62]]

# =====> Write your code here
for mean in myClass:
    for total in myClass[index:]
    total = myClass[2] + myClass[3] + myClass[4] + myClass[5]
    index += 1
    print (total)
This was my attempt at doing it by indexing the first row and moving down by adding one each time to the 2d data structure place. I want to be able to calculate the mean of the last four numbers in the data structure. The numbers would be [2][3][4][5] but I do not know how to calculate it each time for each row on the list. I tried using for mean in myClass[index]: but it would give no extra help. Any ideas
Reply
#2
myClass is a list of students, so your loop could look like
for myStudent in myClass:
    # do something with myStudent
Each student's data is a 1D data structure (a Python list here).
TheOddCircle likes this post
Reply
#3
You might want to "unpack" the student like this:
last_name, first_name, *grades = mystudent
Reply
#4
I would have done something like:
import numpy as np
myClass = [["Lang", "Carla", 49, 71, 95, 50],
           ["Bucklund", "Pia", 86, 78, 83, 64],
           ["Lang", "Jason", 95, 57, 92, 62],
           ["Dimitrousis", "Hector", 93, 45, 89, 96],
           ["Owens", "Sunna", 45, 50, 46, 54],
           ["Goldin", "Sandra", 25, 60, 45, 55],
           ["Giles", "Seth", 67, 73, 93, 64],
           ["Gurillo", "Melanie", 88, 88, 62, 79],
           ["Rykiel", "Kari", 67, 92, 54, 86],
           ["Shailes", "Dennis", 56, 70, 84, 62]]
 
myClass = np.asarray(myClass)

# get numbers only + mean calculations
Notes = myClass[:, 2:].astype(float)
Mean = np.mean(Notes, axis = 1).reshape(-1, 1)


# check status (pass by default)
Status = np.empty( (10,1), dtype='object')
Status[:] = 'Pass'

# now we can look for 'failed' status
t = np.where(Mean < 50)
Status[t] = 'Fail'

# Concat results
myClass = np.hstack((myClass, Mean, Status))
print(f"{myClass}")
Providing:
Output:
[['Lang' 'Carla' '49' '71' '95' '50' 66.25 'Pass'] ['Bucklund' 'Pia' '86' '78' '83' '64' 77.75 'Pass'] ['Lang' 'Jason' '95' '57' '92' '62' 76.5 'Pass'] ['Dimitrousis' 'Hector' '93' '45' '89' '96' 80.75 'Pass'] ['Owens' 'Sunna' '45' '50' '46' '54' 48.75 'Fail'] ['Goldin' 'Sandra' '25' '60' '45' '55' 46.25 'Fail'] ['Giles' 'Seth' '67' '73' '93' '64' 74.25 'Pass'] ['Gurillo' 'Melanie' '88' '88' '62' '79' 79.25 'Pass'] ['Rykiel' 'Kari' '67' '92' '54' '86' 74.75 'Pass'] ['Shailes' 'Dennis' '56' '70' '84' '62' 68.0 'Pass']]
penultimate column = mean
Last column = status
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 1,277 May-17-2022, 11:38 AM
Last Post: Larz60+
Question Change elements of array based on position of input data Cola_Reb 6 937 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  calculate data using 1 byte checksum korenron 2 1,382 Nov-23-2021, 07:17 AM
Last Post: korenron
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 1,761 Apr-23-2021, 08:09 AM
Last Post: sx999
  what data structure to use? Winfried 4 1,821 Mar-16-2021, 12:11 PM
Last Post: buran
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 5,047 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Sorting Elements via parameters pointing to those elements. rpalmer 3 1,697 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  How to use Bunch data structure moish 2 1,737 Dec-24-2020, 06:25 PM
Last Post: deanhystad
  Correct data structure for this problem Wigi 13 2,763 Oct-09-2020, 11:09 AM
Last Post: buran
  difficulties to chage json data structure using json module in python Sibdar 1 1,365 Apr-03-2020, 06:47 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020