PYTHON In this lab, we will be using a file from h... Bookmark PYTHON In this la
ID: 3809338 • Letter: P
Question
PYTHON In this lab, we will be using a file from h... Bookmark PYTHON In this lab, we will be using a file from http://www.databasebasketball.com/stats_download.htm. Download the file and pick out the player_regular_season.csv file which has over 20,000 rows of basketball data from 1946 to 2009. The data in the file is described at http://www.databasebasketball.com/about/aboutstats.htm. Who was the best NBA basketball player? 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 Python3.4. Using the csv file provided with the lab instruction (The links above). 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
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
MUST USE DEF FUNCTION
Please include explaination, because I am pretty stuck on this. In class, we used 'for line in range [0:5]' But that isn't making sense to me. Also must include error checking, but I can do that just fine.
You cannot import anything. To find stuff you must use the range.
Explanation / Answer
Please first manually remove row no. 20314 from the player_regular_season.csv as it is a misplaced row and cannot be taken into as a csv file.
And then, you can follow the commands that i have given.
import os
import re
import pandas as pd
#Reading csv file into pandas.
df = pd.read_csv('player_regular_season.csv')
#Replacing all N, nan, NA with NaN
df['gp']=df['gp'].apply(lambda x: re.sub('N','NaN',str(x)))
df['gp']=df['gp'].apply(lambda x: re.sub('nan','NaN',str(x)))
df['gp']=df['gp'].apply(lambda x: re.sub('NA','NaN',str(x)))
df['gp'] = df['gp'].astype('float')
df['Efficiency'] = ((df['pts'] + df['reb'] + df['asts'] + df['stl'] + df['blk']) - ((df['fga'] - df['fgm']) + (df['fta'] - df['ftm']) + df['turnover'])) / df['gp']
#Making a copy of dataframe df.
df1 = df.copy(deep = True)
# Getting top 50 players based on efficiency in decreasing order.
df1 = df1.sort('Efficiency',ascending = False).head(50)
# Player who played the most minutes.
df2 = df.sort('minutes',ascending = False).head(1)
#Player who played max games.
df3 = df.sort('gp',ascending = False).head(1)
#Player who scored most points.
df4 = df.sort('pts',ascending = False).head(1)
#Player who got most personal fouls.
df5 = df.sort('pf',ascending = False).head(1)
#Player who made the most free throws.
df6 = df.sort('ftm',ascending = False).head(1)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.