Question: Write a academic easy to understand python program that does the following: 1 . Reads the olympics.csv file. Column headers ( Name , Sex, Age,

Write a academic easy to understand python program that does the following:
1. Reads the olympics.csv file. Column headers (Name, Sex, Age, Height, NOC, Year, Season, Sport, Medal)- some of the boxes can have "NA" if no existing data is present.
2. Prints the medal count by decade of age.
3. Prints the average overall age of medal winners of each medal type
You may not use any of the following pandas functions: group_by(), pivot_table(), read_csv().
I would like the python code to be formatted as the example below:
Example:
import pandas as pd
import csv
class Vehicle:
def __init__(self, make, model, year, mpgCity, mpgHwy):
self.make = make
self.model = model
self.year = year
self.mpgCity = int(float(mpgCity.strip()))
self.mpgHwy =int(float(mpgHwy.strip()))
def __str__(self):
return self.make +""+ self.model +""+ self.year +""+ str(self.mpgCity)+""+ str(self.mpgHwy)
mpgCity = pd.Series()
mpgCityAvg = pd.Series()
mpgHwy = pd.Series()
mpgHwyAvg = pd.Series()
with open('mpg.csv') as infile:
reader = csv.reader(infile)
reader.__next__()
for rec in reader:
veh = Vehicle(rec[4], rec[5], rec[10], rec[7], rec[8])
if veh.make in mpgCity:
mpgCity[veh.make]= mpgCity[veh.make]+1
mpgCityAvg[veh.make]= mpgCityAvg[veh.make]+ veh.mpgCity
mpgHwy[veh.make]= mpgHwy[veh.make]+1
mpgHwyAvg[veh.make]= mpgHwyAvg[veh.make]+ veh.mpgHwy
else:
mpgCity[veh.make]=1
mpgCityAvg[veh.make]= veh.mpgCity
mpgHwy[veh.make]=1
mpgHwyAvg[veh.make]= veh.mpgHwy
mpgCityAvg =(mpgCityAvg / mpgCity).agstype(int)
print(mpgCityAvg.to_string())
print('---------------------------------------------------------------')
mpgHwyAvg =(mpgHwyAvg / mpgHwy).astype(int)
print(mpgHwyAvg.to_string())
df = pd.DataFrame({'mpg city':mpgCityAvg, 'mpg hwy':mpgHwyAvg})
print(df)
mpgAvg =((mpgCityAvg + mpgHwyAvg)/2).astype(int)
print(mpgAvg)
df['mpg avg']= mpgAvg
print(df)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!