Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using python, I want solution of task2 using the same ideas of task1 provided. thanks You are providee with two text files. Thye first file,
using python, I want solution of task2 using the same ideas of task1 provided. thanks
You are providee with two text files. Thye first file, named 'Car_sales.txt', contains technical and commercial information about some cars. The following figuer is a screenshot of the file. Columns are separated by a tab ('\ t'). The first line represents the header while the following lines represent the data. The second file, named 'car_list.txt", contains the list of car names. Each name is written in a seprate file. Manufacturer Acura Model Integra TL CL RL AG AB Audi Audi BMW BMW BMW Sales_in_thousands _year_resale_value Vehicle_type Price_in_thousands Engine_size Horsepower Wheelbase Width Length urb_weigtsel_capac Fuel efficiency latest_Launch Power_pert_factor 16.919 16.36 Passenger 21.5 1.8 140 2012 1724 39.384 2/2/2012 19.875 Passenger 28.4 3.2 58.20014952 225 1929 3.517 172 14. 114 6/3/2011 18.225 Passenger 225 91.37077766 1.47 3.58 29.725 1/4/2012 Passenger 3.5 714 3.85 18 20.397 22 1/10/2011 91.38977933 22.255 Passenger 23.99 150 1026 2290 164 27 18.70 10/8/2011 21.555 62.7775392 Passenger 2.8 200 100% 15 2.38 8/9/2011 | Passenger 42 310 113 14.56510502 23.7 19.747 21 2/27/2012 Passenger 2.5 107.3 134.6568582 17 3.179 25 9.231 28.675 6/28/2011 71.19120671 Passenger 193 1023 1N 3.197 16,6 24 1/29/2012 17527 81.87706856 26.125 Passenger 38.9 28 3.472 18.5 91.561 4/4/2011 83.9987218 12.475 Passenger 21.975 3.1 109 72.7 1946 175 11/2/2011 71.18145132 13.74 Passenger 38 240 72 175 27.851 20:19 23 Passenger 9/3/2011 31. 28 205 1118 95.63670253 27 2058 3.78 185 24 12.16 3/23/2012 85.82540825 Passenger 38 205 75 3.591 175 28 63.729 7/23/2011 84.25452581 22.525 Passenger 39.895 46 225 115) 25 2072 3.978 185 15. 27.1 2/23/2012 Passenger 113.8545976 44.475 4.6 1122 201 185 25.725 4/29/2011 115.6213578 Passenger 39.665 100 755 19 11.15 18.225 Passenger 11/27/2011 31.01 3 200 1024 113.7658739 19 1.77 22 9/28/2011 14.78 83.48309358 Car 46.225 5.7 1125 77 5.572 30 15 4/17/2012 109.5091165 Century Regal Park Avenue Sabre Deville Buick Cadillac Cadillac Cadillac Cadillac Eldorado Catera Escalade Objectives: 1. Calculate the total and the average sales in thousands) for each auto manufacturer (Task 1). Show the results as follows: Car name Total sales Average sales 0 Acura 64.891 21.630333 1 Audi 40.557 13.519000 13.379000 N BMW 26.758 3 Buick 242.019 60.504750 2. Find the best-selling car model for each car manufacturer (Task 2). Organize the results in a csv file. It should look like the following: Car name Best selling 0 Acura TL 1 Audi A4 2 BMW 528i 3 Buick Century De Ville 4 Cadillac 5 Chevrolet 6 Chrysler 7 Dodge 8 Ford Cavalier Sebring Conv. Ram Pickup F-Series Accord Elantra 9 Honda 10 Hyundai 11 Infiniti 130 12 Jeep 13 Lexus Grand Cherokee ES300 Town car 14 Lincoln Define the 1st functiuon i def getDataAboutCar(in_data_file, in_car, in_key): 2 3 4 5 6 7 8 9 10 11 12 This function find information about a specific car input: @ in_data_file: name of the file containing data @ in_car: name of the car @ in_key: target information output: @ ou_tots: total sales @ ou_avgs: average sales 10/5/2020 DCS, SQU 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # --> # open data file file = open(in_data_file) # read the header (1st line) line = file.readline().strip().split('\t') # get the index (column number) of the key infoirmation indx_key = line.index(in_key) # read data from next lines total_sales = # used to compute the total sales i = 0 # used to compute the number of car's model for line in file: line = line.strip().split('\t') car_name = line[@] if car_name. lower() -- car.lower(): total_sales = total_sales + float(line[indx_key]) i = 1+1 30 31 32 33 34 35 36 37 38 average_sales = total_sales/i ou_tots = total_sales ou_avgs = average_sales # close file file.close() return ou_tots, ou_avgs Test for one car, i.e., Acura 1 car = input('Which car manufacturer would you like to look up for sales information?') total sales. average sales = getDataAboutCar(data file.car. 'Sales in thousands') 2 3 print('Total sales = %2.2f and Average sale = %2.2f'%(total_sales, average_sales)) Which car manufacturer would you like to look up for sales information?acura Total sales = 64.89 and Average sale = 21.63 Define the 2nd function 3 i def compute TotalSales ByCarManufacturer(in_data_file, in_car_list, in_key): 2 The function compute the total sales for all the car manufacturers. Input: @ in_data_file: data file @ in_car_list: car list @ in_key: target information Output: 9 @ ou_ds: dictinary of sale information 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 car_file = open(in_car_list, 'r') dict_sales = {} car_names = [] total_sales = [] average_sales = [] for car in car_file: car_name = car.strip().split()[0] car_names.append(car_name) # call the first function tots, avgs = getDataAboutCar(in_data_file,car_name, in_key) total_sales.append(tots) average sales, annend (ayes) 20 21 22 20 21 22 23 tots, avgs = getDataAboutCar(in_data_file, car_name, in_key) total_sales.append(tots) average_sales.append(avgs) dict_sales['Car name'] = car_names dict_sales['Total sales'] = total_sales dict_sales['Average sales'] = average_sales 24 25 26 27 28 ou_ds = dict_sales car_file.close() return dict_sales 29 Test for all cars 1 2 dict_sales = computeTotalSalesByCarManufacturer(data_file, car_list, 'Sales_in_thousands' print(dict_sales) Record the result in a CSV format file 1 2 3 import pandas as pd df = pd.DataFrame (dict_sales) df.head (5) # save it as csv file df.to_csv('salesbyCars.csv') 4 5 Task 2 1 2 def getBestsellingCar(i_data_file, i_car): T! 3 4 1 2 input('Enter the car manufacturer name for which you want to find the best selling: car = #.. def bestsellingByCarManufacturer(i_data_file, i_car_list): 1 2 3 T 4 1 2 best selling_all_cars = print(best_selling_all_cars) You are providee with two text files. Thye first file, named 'Car_sales.txt', contains technical and commercial information about some cars. The following figuer is a screenshot of the file. Columns are separated by a tab ('\ t'). The first line represents the header while the following lines represent the data. The second file, named 'car_list.txt", contains the list of car names. Each name is written in a seprate file. Manufacturer Acura Model Integra TL CL RL AG AB Audi Audi BMW BMW BMW Sales_in_thousands _year_resale_value Vehicle_type Price_in_thousands Engine_size Horsepower Wheelbase Width Length urb_weigtsel_capac Fuel efficiency latest_Launch Power_pert_factor 16.919 16.36 Passenger 21.5 1.8 140 2012 1724 39.384 2/2/2012 19.875 Passenger 28.4 3.2 58.20014952 225 1929 3.517 172 14. 114 6/3/2011 18.225 Passenger 225 91.37077766 1.47 3.58 29.725 1/4/2012 Passenger 3.5 714 3.85 18 20.397 22 1/10/2011 91.38977933 22.255 Passenger 23.99 150 1026 2290 164 27 18.70 10/8/2011 21.555 62.7775392 Passenger 2.8 200 100% 15 2.38 8/9/2011 | Passenger 42 310 113 14.56510502 23.7 19.747 21 2/27/2012 Passenger 2.5 107.3 134.6568582 17 3.179 25 9.231 28.675 6/28/2011 71.19120671 Passenger 193 1023 1N 3.197 16,6 24 1/29/2012 17527 81.87706856 26.125 Passenger 38.9 28 3.472 18.5 91.561 4/4/2011 83.9987218 12.475 Passenger 21.975 3.1 109 72.7 1946 175 11/2/2011 71.18145132 13.74 Passenger 38 240 72 175 27.851 20:19 23 Passenger 9/3/2011 31. 28 205 1118 95.63670253 27 2058 3.78 185 24 12.16 3/23/2012 85.82540825 Passenger 38 205 75 3.591 175 28 63.729 7/23/2011 84.25452581 22.525 Passenger 39.895 46 225 115) 25 2072 3.978 185 15. 27.1 2/23/2012 Passenger 113.8545976 44.475 4.6 1122 201 185 25.725 4/29/2011 115.6213578 Passenger 39.665 100 755 19 11.15 18.225 Passenger 11/27/2011 31.01 3 200 1024 113.7658739 19 1.77 22 9/28/2011 14.78 83.48309358 Car 46.225 5.7 1125 77 5.572 30 15 4/17/2012 109.5091165 Century Regal Park Avenue Sabre Deville Buick Cadillac Cadillac Cadillac Cadillac Eldorado Catera Escalade Objectives: 1. Calculate the total and the average sales in thousands) for each auto manufacturer (Task 1). Show the results as follows: Car name Total sales Average sales 0 Acura 64.891 21.630333 1 Audi 40.557 13.519000 13.379000 N BMW 26.758 3 Buick 242.019 60.504750 2. Find the best-selling car model for each car manufacturer (Task 2). Organize the results in a csv file. It should look like the following: Car name Best selling 0 Acura TL 1 Audi A4 2 BMW 528i 3 Buick Century De Ville 4 Cadillac 5 Chevrolet 6 Chrysler 7 Dodge 8 Ford Cavalier Sebring Conv. Ram Pickup F-Series Accord Elantra 9 Honda 10 Hyundai 11 Infiniti 130 12 Jeep 13 Lexus Grand Cherokee ES300 Town car 14 Lincoln Define the 1st functiuon i def getDataAboutCar(in_data_file, in_car, in_key): 2 3 4 5 6 7 8 9 10 11 12 This function find information about a specific car input: @ in_data_file: name of the file containing data @ in_car: name of the car @ in_key: target information output: @ ou_tots: total sales @ ou_avgs: average sales 10/5/2020 DCS, SQU 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # --> # open data file file = open(in_data_file) # read the header (1st line) line = file.readline().strip().split('\t') # get the index (column number) of the key infoirmation indx_key = line.index(in_key) # read data from next lines total_sales = # used to compute the total sales i = 0 # used to compute the number of car's model for line in file: line = line.strip().split('\t') car_name = line[@] if car_name. lower() -- car.lower(): total_sales = total_sales + float(line[indx_key]) i = 1+1 30 31 32 33 34 35 36 37 38 average_sales = total_sales/i ou_tots = total_sales ou_avgs = average_sales # close file file.close() return ou_tots, ou_avgs Test for one car, i.e., Acura 1 car = input('Which car manufacturer would you like to look up for sales information?') total sales. average sales = getDataAboutCar(data file.car. 'Sales in thousands') 2 3 print('Total sales = %2.2f and Average sale = %2.2f'%(total_sales, average_sales)) Which car manufacturer would you like to look up for sales information?acura Total sales = 64.89 and Average sale = 21.63 Define the 2nd function 3 i def compute TotalSales ByCarManufacturer(in_data_file, in_car_list, in_key): 2 The function compute the total sales for all the car manufacturers. Input: @ in_data_file: data file @ in_car_list: car list @ in_key: target information Output: 9 @ ou_ds: dictinary of sale information 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 car_file = open(in_car_list, 'r') dict_sales = {} car_names = [] total_sales = [] average_sales = [] for car in car_file: car_name = car.strip().split()[0] car_names.append(car_name) # call the first function tots, avgs = getDataAboutCar(in_data_file,car_name, in_key) total_sales.append(tots) average sales, annend (ayes) 20 21 22 20 21 22 23 tots, avgs = getDataAboutCar(in_data_file, car_name, in_key) total_sales.append(tots) average_sales.append(avgs) dict_sales['Car name'] = car_names dict_sales['Total sales'] = total_sales dict_sales['Average sales'] = average_sales 24 25 26 27 28 ou_ds = dict_sales car_file.close() return dict_sales 29 Test for all cars 1 2 dict_sales = computeTotalSalesByCarManufacturer(data_file, car_list, 'Sales_in_thousands' print(dict_sales) Record the result in a CSV format file 1 2 3 import pandas as pd df = pd.DataFrame (dict_sales) df.head (5) # save it as csv file df.to_csv('salesbyCars.csv') 4 5 Task 2 1 2 def getBestsellingCar(i_data_file, i_car): T! 3 4 1 2 input('Enter the car manufacturer name for which you want to find the best selling: car = #.. def bestsellingByCarManufacturer(i_data_file, i_car_list): 1 2 3 T 4 1 2 best selling_all_cars = print(best_selling_all_cars)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started