Westonci.ca is the premier destination for reliable answers to your questions, brought to you by a community of experts. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts. Get detailed and accurate answers to your questions from a dedicated community of experts on our Q&A platform.

Write a program to read from std_info.txt.
This file has student first name, last name, major, and gpa.
This program must compute the average gpa of ee, cpe, and all students in the file. you must write in student_avg.txt file, the student information and computed gpa at the bottom of the list of students in the following order:
Sam Thomas CPE 3.76Mary Smith EE 2.89John Jones BUS 4.00....EE average =CPE average =Total average =


Sagot :

Answer:

import pandas as pd

# loads the text file as a pandas dataframe

student_file = pd.read_fwf("std_info.txt")

# opens a new text file if the student_avg does not exist

# the file closes automatically at the end of the with statement

with open('student_avg.txt', 'w+') as file:

   for row in student_file.iterrows():

       file.write(row)

   ee = student_file[student_file['major'=='EE']]

   cpe = student_file[student_file['major'=='CPE']]

   file.write(f'EE average = {ee['EE'].mean()}')

   file.write(f'CPE average = {ee['CPE'].mean()}')

   file.write(f'Total average = {student_file['EE'].mean()}')

Explanation:

The python program gets the text file as a fixed-width file and loads the file as a pandas dataframe. The dataframe is used to get the total average GPA the student GPA and the average GPA of students in various departments. The results are saved in a new file called 'student_avg.txt'.