Hello, I am really stuck on this Python project. Can anyone solve it? The NBA is
ID: 3809249 • Letter: H
Question
Hello, I am really stuck on this Python project. Can anyone solve it?
The NBA is North America’s professional men’s basketball league. You are going to write a program to find the best players in NBA history using Python 3.6. Calculate the efficiency for each player, using the csv file provided with the lab. NBA player’s performance is evaluated using the following efficiency formula. Efficiency = ( ( pts + reb + asts + stl + blk ) - ( ( fga – fgm ) + ( fta – ftm ) + turnover ) ) / gp. The file I have to work with is named player_regular_season.csv using data from (http://www.databasebasketball.com/about/aboutstats.htm)
1. Find the top 50 players with the best efficiency results and Display those 50 from best to worst.
2. Find and display the Player who played the most minutes
3. Find and display the player who play the most games
4. Find and display the Player who scored the most points.
5. Find and display the Player who made the most free throws.
Remember:
All indexes start on ZERO.
The First line in the file provides the labels of the columns
The File can be downloaded from here: http://www.databasebasketball.com/stats_download.htm
Explanation / Answer
import csv
import operator
values_list = []
efficiency_val = {}
most_games = {}
most_minutes = {}
most_points = {}
most_freethrows={}
'''Reading the contents of csv as list of lists'''
with open('/home/naresh/Downloads/databasebasketball_2009_v1/player_regular_season.csv', 'rb') as f:
next(f) # To skip the header while reading csv
reader = csv.reader(f)
values_list = list(reader)
for val in values_list:
'''Calculating the efficiency values for all the players'''
try:
if val[0] not in efficiency_val:
try:
efficiency_val[val[0]] = ((float(val[8]) + float(val[11]) + float(val[12]) + float(val[13]) + float(val[14])) -((float(val[17]) - float(val[18])) + ( float(val[19]) - float(val[20])) + float(val[15])))/float(val[6])
except Exception as e:
pass
else:
pass
except Exception as e:
pass
'''Calculating the the number of minutes played for all the players'''
try:
if val[0] not in most_minutes:
try:
most_minutes[val[0]] = int(val[7])
except Exception as e:
pass
else:
pass
except Exception as e:
pass
'''Calculating the number of games played for all the players'''
try:
if val[0] not in most_games:
try:
most_games[val[0]] = int(val[6])
except Exception as e:
pass
else:
pass
except Exception as e:
pass
'''Calculating the number of points scored for all the players'''
try:
if val[0] not in most_points:
try:
most_points[val[0]] = int(val[8])
except Exception as e:
pass
else:
pass
except Exception as e:
pass
'''Calculating the number of free throws scored for all the players'''
try:
if val[0] not in most_freethrows:
try:
most_freethrows[val[0]] = int(val[20])
except Exception as e:
pass
else:
pass
except Exception as e:
pass
sorted_efficiency_values = sorted(efficiency_val.items(), key=operator.itemgetter(1))
print("----------Top %0 Plyers with Most Effiency-------------")
print(sorted_efficiency_values[-51:])
print(" ")
most_minutes_sorted = sorted(most_minutes.items(), key=operator.itemgetter(1))
print("The Player with most minutes")
print(most_minutes_sorted[-1])
print(" ")
most_games_sorted = sorted(most_games.items(), key=operator.itemgetter(1))
print("The Player with most Games")
print(most_games_sorted[-1])
print(" ")
most_points_sorted = sorted(most_points.items(), key=operator.itemgetter(1))
print("The Player with most points")
print(most_points_sorted[-1])
print(" ")
most_freethrows_sorted = sorted(most_freethrows.items(), key=operator.itemgetter(1))
print("The Player with most freethrows")
print(most_freethrows_sorted[-1])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.