Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

from datascience import *
import numpy as np

%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')

NBA Salaries

nba = Table.read_table('nba_salaries.csv').where('season', 2020).drop('rank', 'season')
nba.show(3)
nba.where('position', 'C').show(2)
nba.where('position', are.equal_to('C')).show(2)
nba.where('position', are.not_equal_to('C')).show(2)
nba.where('salary', are.between(3e6, 5e6)).show(2)
nba.where('salary', are.between(3e6, 5e6)).num_rows / nba.num_rows

Census

full = Table.read_table('nc-est2019-agesex-res.csv')
full.show(5)
partial = full.select('SEX', 'AGE', 'POPESTIMATE2014', 'POPESTIMATE2019')
partial.show(5)
us_pop = partial.relabeled(2, '2014').relabeled(3, '2019')
us_pop.show(5)
us_pop.where('AGE', are.above_or_equal_to(100)).sort('AGE')

2019 Sex Ratios

us_pop_2019 = us_pop.drop('2014')
us_pop_2019.show(3)
all_ages = us_pop_2019.where('AGE', are.equal_to(999))
all_ages
infants = us_pop_2019.where('AGE', are.equal_to(0))
infants
females_all_rows = us_pop_2019.where('SEX', are.equal_to(2))
females = females_all_rows.where('AGE', are.not_equal_to(999))
females.show(3)
males_all_rows = us_pop_2019.where('SEX', are.equal_to(1))
males = males_all_rows.where('AGE', are.not_equal_to(999))
males.show(3)
f_to_m_ratios = females.column(2) / males.column(2)

ratios = Table().with_columns(
    'Age', females.column('AGE'),
    'F:M Ratio', f_to_m_ratios
)

ratios
ratios.sort('Age', descending=True)

Line Plot

ratios.plot('Age', 'F:M Ratio')