NBA additional info

For the following exercises, we'll read nba_data.csv CSV file, that contains data of NBA Players (original can be found here).

Your task:

a) The column Fg% is the "Field Goals percentage", a measure of accuracy for Field Goals. Use the columns Fg (Field Goals) and Fga (Field Goal attempts) to calculate our own Fg%: create a new column named Fg% Calculated.
b) Using the previous column Fg% Calculated (check the solution if you didn't solve it yet), create a new column Fg% Error, that's the result of np.abs(Fg% - Fg% Calculated).
c) Create a new column Defensive Score, with the following formula: Defensive Score = 2*Drb (Defensive Rebounds) + 5 Stl (Steals) - 1.5 Tov (Turnovers).

Test Cases

test fg error - Run Test

def test_fg_error():
    assert round(df['Fg% Error'].sum()) == 2.0 and round(df.loc[416, 'Fg% Error'], 2) == 0.04

test defensive score - Run Test

def test_defensive_score():
    assert round(df['Defensive Score'].sum()) == 3173.0 and round(df.loc[315, 'Defensive Score']) == 6.0

test fg calculated - Run Test

def test_fg_calculated():
    assert round(df['Fg% Calculated'].sum()) == 198.0 and round(df.loc[97,'Fg% Calculated']) == 1.0

Solution 1

import numpy as np
import pandas as pd

df = pd.read_csv('nba_data.csv')

# create 'Fg% Calculated'
df['Fg% Calculated'] = df['Fg'] / df['Fga']

# create 'Fg% Error'
df['Fg% Error'] = np.abs(df['Fg%'] - df['Fg% Calculated'])

# create 'Defensive Score'
df['Defensive Score'] = 2 * df['Drb'] + 5 * df['Stl'] - 1.5 * df['Tov']
Files associated with this lesson:


import numpy as np import pandas as pd df = pd.read_csv('nba_data.csv')