Saving the last 3 points and removing senior points and calculating the average?

I am creating a program that opens and reads a csv file and is sorted as follows:

  • Alphabetical order with the highest score of each student.
  • The highest score, from the highest to the lowest.
  • Average score, from maximum to lowest.

The program should store the last 3 grades for each student. This is the part that I'm stuck in and need help with. When sorting a file in alphabetical order, the program should look at each student's last 3 recent grades and select the highest number. My code currently only sorts the file in alphabetical order. He reviews the last 3 ratings and selects the highest. I need help here.

My code already sorts the points from highest to lowest, but it displays all the grades received by each student, and does not print their highest score from their last 3 points.

Andrew 1
Andrew 2
Andrew 3
Andrew 4
Andrew 5

Finally, I need help calculating the average grade for each student. I guess how this should be done by adding Andrei the last 3 points, which are 5, 4 and 3 and are divided by 3.

This is my code:

import csv, operator

selected_class = input("Pick a class file, (5, 6 or 7)? ")

print("1. Alphabetical order.")
print("2. Highest to lowest.")
print("3. Average score.")

selected_sorting = input("Pick an option 1, 2, or 3: ")

class_file = "Class " + selected_class + ".csv"
open_file = open(class_file)
csv_file = csv.reader(open_file)

if selected_sorting == "1":
    sorted_name = sorted(csv_file, key=operator.itemgetter(0))
    for i in sorted_name:
        print(i)

elif selected_sorting == "2":
    sorted_results = sorted(csv_file, key=lambda row: int(row[1]), reverse=True)
    for i in sorted_results:
        print(i)

elif selected_sorting == "3":
+4
source share
2 answers

I will give a code for demonstration:

# -*- coding: utf-8 -*-
import csv
from collections import defaultdict
from statistics import mean

class_file = 'scores.csv'
open_file = open(class_file)
csv_file = csv.reader(open_file)


def main():
    # First, use student name to group by all scores, this will
    # generate structure like this:
    # {
    #     'Andrew': [1, 2, 3, 4, 5]),
    #     'Luck': [10, 20]),
    # }
    score_groups = defaultdict(list)
    for name, score in csv_file:
        score_groups[name].append(int(score))

    # Secondary, use the 3 latest socres only 
    l3_score_groups = [(key, value[-3:]) for key, value in score_groups.items()]

    print('1. Alphabetical order with each students highest score.')
    l3_highest_score_groups = [(key, max(values)) for key, values in l3_score_groups]
    for name, score in sorted(l3_highest_score_groups, key=lambda x: x[0]):
        print(name, score)

    print('2. By the highest score, highest to lowest.')
    l3_highest_score_groups = [(key, max(values)) for key, values in l3_score_groups]
    for name, score in sorted(l3_highest_score_groups, key=lambda x: x[1], reverse=True):
        print(name, score)

    print('3. Average score, highest to lowest.')
    l3_aver_score_groups = [(key, mean(values)) for key, values in l3_score_groups]
    for name, score in sorted(l3_aver_score_groups, key=lambda x: x[1], reverse=True):
        print(name, score)


if __name__ == '__main__':
    main()

Here are the techniques that were used above:

Hope this helps.

+1

pandas ( )

import pandas as pd

dataframe = pd.read_csv(' your file ') 

print dataframe.columns

student1 = dataframe[dataframe['studentnamecolumn']=='Andrew']

last3 = student1.sort('examdatecolumnname').iloc[-3:]

avgscore = last3['examscorecolumn'].mean()

. DataAnalysis Python,

0

Source: https://habr.com/ru/post/1621555/


All Articles