NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import pandas as pd
import numpy as np
import scipy.stats as stats
import re

#mlb_df=pd.read_csv("assets/mlb.csv")
#nhl_df=pd.read_csv("assets/nhl.csv")
#nba_df=pd.read_csv("assets/nba.csv")
#nfl_df=pd.read_csv("assets/nfl.csv")
#cities=pd.read_html("assets/wikipedia_data.html")[1]
#cities=cities.iloc[:-1,[0,3,5,6,7,8]]

def nhl_correla():
# YOUR CODE HERE
#raise NotImplementedError()

nhl_df=pd.read_csv("assets/nhl.csv")
cities=pd.read_html("assets/wikipedia_data.html")[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]

nhl_df.drop([0,9,18,26],0,inplace=True)
cities.drop([14,15,18,19,20,21,23,24,25,27,28,32,33,38,40,41,42,44,45,46,48,49,50],0,inplace=True)

l= []
for i in cities['NHL']:
i=i.split('[')
l.append(i[0])
cities['NHL'] = l

li = []
for i in nhl_df['team']:
i = re.findall("[^*]+", i)
li.append(i[0])
nhl_df['team'] = li

nhl_df = nhl_df.head(31)

nhl_df['team_ville'] = nhl_df['team']
nhl_df['team_ville'] = nhl_df['team_ville'].map({'Tampa Bay Lightning':'Tampa Bay Area',
'Boston Bruins':'Boston',
'Toronto Maple Leafs':'Toronto',
'Florida Panthers':'Miami–Fort Lauderdale',
'Detroit Red Wings':'Detroit',
'Montreal Canadiens':'Montreal',
'Ottawa Senators':'Ottawa',
'Buffalo Sabres':'Buffalo',
'Washington Capitals':'Washington, D.C.',
'Pittsburgh Penguins':'Pittsburgh',
'Philadelphia Flyers':'Philadelphia',
'Columbus Blue Jackets':'Columbus',
'New Jersey Devils':'New York City',
'Carolina Hurricanes':'Raleigh',
'New York Islanders':'New York City',
'New York Rangers':'New York City',
'Nashville Predators':'Nashville',
'Winnipeg Jets':'Winnipeg',
'Minnesota Wild':'Minneapolis–Saint Paul',
'Colorado Avalanche':'Denver',
'St. Louis Blues':'St. Louis',
'Dallas Stars':'Dallas–Fort Worth',
'Chicago Blackhawks':'Chicago',
'Vegas Golden Knights':'Las Vegas',
'Anaheim Ducks':'Los Angeles',
'San Jose Sharks':'San Francisco Bay Area',
'Los Angeles Kings':'Los Angeles',
'Calgary Flames':'Calgary',
'Edmonton Oilers':'Edmonton',
'Vancouver Canucks':'Vancouver',
'Arizona Coyotes':'Phoenix'})

df = pd.merge(nhl_df,cities, left_on= "team_ville", right_on= "Metropolitan area")

df['W'] = pd.to_numeric(df['W'])
df['L'] = pd.to_numeric(df['L'])
df['Population (2016 est.)[8]'] = pd.to_numeric(df['Population (2016 est.)[8]'])

he = ['team','W','L','Metropolitan area','Population (2016 est.)[8]']

df = df[he]

df['W/L'] = df['W']/(df['L']+df['W'])

df = df.groupby('Metropolitan area').mean().reset_index()

return df

def nba_correla():
# YOUR CODE HERE
#raise NotImplementedError()

nba_df=pd.read_csv("assets/nba.csv")
cities=pd.read_html("assets/wikipedia_data.html")[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]

cities.drop([16,17,19,20,21,22,23,26,29,30,31,34,35,36,37,39,40,43,44,47,48,49,50],0,inplace=True)

l1 = []
for i in nba_df['team']:
#i=i.rstrip()
i=i.split('*')
l1.append(i[0])
nba_df['team'] = l1

l2 = []
for i in nba_df['team']:
i=i.split('(')
l2.append(i[0])
nba_df['team'] = l2

l3 = []
for i in nba_df['team']:
i=i.rstrip()
l3.append(i)
nba_df['team'] = l3



nba_df = nba_df.head(30)

nba_df['team_ville'] = nba_df['team']
nba_df['team_ville'] = nba_df['team_ville'].map({'Toronto Raptors':'Toronto',
'Boston Celtics':'Boston',
'Philadelphia 76ers':'Philadelphia',
'Cleveland Cavaliers':'Cleveland',
'Indiana Pacers':'Indianapolis',
'Miami Heat':'Miami–Fort Lauderdale',
'Milwaukee Bucks':'Milwaukee',
'Washington Wizards':'Washington, D.C.',
'Detroit Pistons':'Detroit',
'Charlotte Hornets':'Charlotte',
'New York Knicks':'New York City',
'Brooklyn Nets':'New York City',
'Chicago Bulls':'Chicago',
'Orlando Magic':'Orlando',
'Atlanta Hawks':'Atlanta',
'Houston Rockets':'Houston',
'Golden State Warriors':'San Francisco Bay Area',
'Portland Trail Blazers':'Portland',
'Oklahoma City Thunder':'Oklahoma City',
'Utah Jazz':'Salt Lake City',
'New Orleans Pelicans':'New Orleans',
'San Antonio Spurs':'San Antonio',
'Minnesota Timberwolves':'Minneapolis–Saint Paul',
'Denver Nuggets':'Denver',
'Los Angeles Clippers':'Los Angeles',
'Los Angeles Lakers':'Los Angeles',
'Sacramento Kings':'Sacramento',
'Dallas Mavericks':'Dallas–Fort Worth',
'Memphis Grizzlies':'Memphis',
'Phoenix Suns':'Phoenix'})

df2 = pd.merge(nba_df,cities, left_on= "team_ville", right_on= "Metropolitan area")

df2['W/L%'] = pd.to_numeric(df2['W/L%'])
df2['W'] = pd.to_numeric(df2['W'])
df2['L'] = pd.to_numeric(df2['L'])
df2['Population (2016 est.)[8]'] = pd.to_numeric(df2['Population (2016 est.)[8]'])
he = ['team','W','L','W/L%','Metropolitan area','Population (2016 est.)[8]']
df2 = df2[he]
df2['W/L'] = df2['W']/(df2['L']+df2['W'])
df2 = df2.groupby('Metropolitan area').mean().reset_index()

return df2

def mlb_correla():
# YOUR CODE HERE
#raise NotImplementedError()

mlb_df=pd.read_csv("assets/mlb.csv")
cities=pd.read_html("assets/wikipedia_data.html")[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]

cities.drop([24,25,26,28,29,30,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,50],0,inplace=True)

mlb_df = mlb_df.head(30)

mlb_df['team_ville'] = mlb_df['team']
mlb_df['team_ville'] = mlb_df['team_ville'].map({'Boston Red Sox':'Boston',
'New York Yankees':'New York City',
'Tampa Bay Rays':'Tampa Bay Area',
'Toronto Blue Jays':'Toronto',
'Baltimore Orioles':'Baltimore',
'Cleveland Indians':'Cleveland',
'Minnesota Twins':'Minneapolis–Saint Paul',
'Detroit Tigers':'Detroit',
'Chicago White Sox':'Chicago',
'Kansas City Royals':'Kansas City',
'Houston Astros':'Houston',
'Oakland Athletics':'San Francisco Bay Area',
'Seattle Mariners':'Seattle',
'Los Angeles Angels':'Los Angeles',
'Texas Rangers':'Dallas–Fort Worth',
'Atlanta Braves':'Atlanta',
'Washington Nationals':'Washington, D.C.',
'Philadelphia Phillies':'Philadelphia',
'New York Mets':'New York City',
'Miami Marlins':'Miami–Fort Lauderdale',
'Milwaukee Brewers':'Milwaukee',
'Chicago Cubs':'Chicago',
'St. Louis Cardinals':'St. Louis',
'Pittsburgh Pirates':'Pittsburgh',
'Cincinnati Reds':'Cincinnati',
'Los Angeles Dodgers':'Los Angeles',
'Colorado Rockies':'Denver',
'Arizona Diamondbacks':'Phoenix',
'San Francisco Giants':'San Francisco Bay Area',
'San Diego Padres':'San Diego'})

df3 = pd.merge(mlb_df,cities, left_on= "team_ville", right_on= "Metropolitan area")

#df2['W/L%'] = pd.to_numeric(df2['W/L%'])
df3['W'] = pd.to_numeric(df3['W'])
df3['L'] = pd.to_numeric(df3['L'])
df3['Population (2016 est.)[8]'] = pd.to_numeric(df3['Population (2016 est.)[8]'])
he = ['team','W','L','Metropolitan area','Population (2016 est.)[8]']
df3 = df3[he]
df3['W/L'] = df3['W']/(df3['L']+df3['W'])
df3 = df3.groupby('Metropolitan area').mean().reset_index()

return df3

def nfl_correla():
# YOUR CODE HERE
#raise NotImplementedError()

nfl_df=pd.read_csv("assets/nfl.csv")
cities=pd.read_html("assets/wikipedia_data.html")[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]

nfl_df.drop([0,5,10,15,20,25,30,35],0,inplace=True)

cities.drop([13,22,27,30,31,32,33,34,35,36,37,38,39,40,41,42,43,45,46,47,49,50],0,inplace=True)

l1 = []
for i in nfl_df['team']:
#i=i.rstrip()
i=i.split('*')
l1.append(i[0])
nfl_df['team'] = l1

l2 = []
for i in nfl_df['team']:
i=i.split('+')
l2.append(i[0])
nfl_df['team'] = l2

nfl_df = nfl_df.head(32)

nfl_df['team_ville'] = nfl_df['team']
nfl_df['team_ville'] = nfl_df['team_ville'].map({'New England Patriots':'Boston',
'Miami Dolphins':'Miami–Fort Lauderdale',
'Buffalo Bills':'Buffalo',
'New York Jets':'New York City',
'Baltimore Ravens':'Baltimore',
'Pittsburgh Steelers':'Pittsburgh',
'Cleveland Browns':'Cleveland',
'Cincinnati Bengals':'Cincinnati',
'Houston Texans':'Houston',
'Indianapolis Colts':'Indianapolis',
'Tennessee Titans':'Nashville',
'Jacksonville Jaguars':'Jacksonville',
'Kansas City Chiefs':'Kansas City',
'Los Angeles Chargers':'Los Angeles',
'Denver Broncos':'Denver',
'Oakland Raiders':'San Francisco Bay Area',
'Dallas Cowboys':'Dallas–Fort Worth',
'Philadelphia Eagles':'Philadelphia',
'Washington Redskins':'Washington, D.C.',
'New York Giants':'New York City',
'Chicago Bears':'Chicago',
'Minnesota Vikings':'Minneapolis–Saint Paul',
'Green Bay Packers':'Green Bay',
'Detroit Lions':'Detroit',
'New Orleans Saints':'New Orleans',
'Carolina Panthers':'Charlotte',
'Atlanta Falcons':'Atlanta',
'Tampa Bay Buccaneers':'Tampa Bay Area',
'Los Angeles Rams':'Los Angeles',
'Seattle Seahawks':'Seattle',
'San Francisco 49ers':'San Francisco Bay Area',
'Arizona Cardinals':'Phoenix'})

df4 = pd.merge(nfl_df,cities, left_on= "team_ville", right_on= "Metropolitan area")


df4['W'] = pd.to_numeric(df4['W'])
df4['L'] = pd.to_numeric(df4['L'])
df4['Population (2016 est.)[8]'] = pd.to_numeric(df4['Population (2016 est.)[8]'])
he = ['team','W','L','Metropolitan area','Population (2016 est.)[8]']
df4 = df4[he]
df4['W/L'] = df4['W']/(df4['L']+df4['W'])
df4 = df4.groupby('Metropolitan area').mean().reset_index()

return df4





def sports_team_performance():
# YOUR CODE HERE
#raise NotImplementedError()

nfl = nfl_correla()
nba = nba_correla()
mlb = mlb_correla()
nhl = nhl_correla()

nba_nfl = pd.merge(nba,nfl, on='Metropolitan area')
pval_nba_nfl = stats.ttest_rel(nba_nfl['W/L_x'],nba_nfl['W/L_y'])[1]
nba_nhl = pd.merge(nba,nhl, on='Metropolitan area')
pval_nba_nhl = stats.ttest_rel(nba_nhl['W/L_x'],nba_nhl['W/L_y'])[1]
mlb_nfl = pd.merge(mlb,nfl, on='Metropolitan area')
pval_mlb_nfl = stats.ttest_rel(mlb_nfl['W/L_x'],mlb_nfl['W/L_y'])[1]
mlb_nhl = pd.merge(mlb,nhl, on='Metropolitan area')
pval_mlb_nhl = stats.ttest_rel(mlb_nhl['W/L_x'],mlb_nhl['W/L_y'])[1]
mlb_nba = pd.merge(mlb,nba, on='Metropolitan area')
pval_mlb_nba = stats.ttest_rel(mlb_nba['W/L_x'],mlb_nba['W/L_y'])[1]
nhl_nfl = pd.merge(nhl,nfl, on='Metropolitan area')
pval_nhl_nfl = stats.ttest_rel(nhl_nfl['W/L_x'],nhl_nfl['W/L_y'])[1]

pv = {'NFL': {"NFL": np.nan, 'NBA': pval_nba_nfl, 'NHL': pval_nhl_nfl, 'MLB': pval_mlb_nfl},
'NBA': {"NFL": pval_nba_nfl, 'NBA': np.nan, 'NHL': pval_nba_nhl, 'MLB': pval_mlb_nba},
'NHL': {"NFL": pval_nhl_nfl, 'NBA': pval_nba_nhl, 'NHL': np.nan, 'MLB': pval_mlb_nhl},
'MLB': {"NFL": pval_mlb_nfl, 'NBA': pval_mlb_nba, 'NHL': pval_mlb_nhl, 'MLB': np.nan}
}


# Note: p_values is a full dataframe, so df.loc["NFL","NBA"] should be the same as df.loc["NBA","NFL"] and
# df.loc["NFL","NFL"] should return np.nan
#sports = ['NFL', 'NBA', 'NHL', 'MLB']
#p_values = pd.DataFrame({k:np.nan for k in sports}, index=sports)
p_values = pd.DataFrame(pv)

assert abs(p_values.loc["NBA", "NHL"] - 0.02) <= 1e-2, "The NBA-NHL p-value should be around 0.02"
assert abs(p_values.loc["MLB", "NFL"] - 0.80) <= 1e-2, "The MLB-NFL p-value should be around 0.80"
return p_values
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.