9.11. Case Study HTML ESPN

../../_images/html-espn-standing.png

9.11.1. Code

import pandas as pd

DATA = 'https://python3.info/_static/espn-standing.html'
# DATA = 'https://www.espn.com/nba/standings/_/group/league'

tables = pd.read_html(DATA)
teams = tables[0]
scores = tables[1]

first = pd.Series(teams.columns)
teams = pd.concat((first, teams.iloc[:, 0])).reset_index(drop=True)
teams = pd.DataFrame(teams, columns=['Team'])

result = teams.join(scores)
result['Team'] = result['Team'].str.replace(r'[A-Z]+([A-Z])', r'\1', regex=True)

q = result['Team'] == 'A Clippers'
result.loc[q, 'Team'] = 'LA Clippers'

9.11.2. Assignments

# %% About
# - Name: Pandas CaseStudy ESPN
# - Difficulty: medium
# - Lines: 10
# - Minutes: 21

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Create `result: pd.DataFrame` for input data
# 2. Run doctests - all must succeed

# %% Polish
# 1. Stwórz `result: pd.DataFrame` dla danych wejściowych
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - Use selection with `alt` key in your IDE

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.DataFrame, \
'Variable `result` has an invalid type; expected: `pd.DataFrame`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result  # doctest: +NORMALIZE_WHITESPACE
                      Team   W   L    PCT   GB HOME AWAY  DIV  CONF    PPG  OPP PPG  DIFF STRK  L10
0       Los Angeles Lakers  11   4  0.733    -  4-4  7-0  0-2  10-4  115.3    105.1  10.2   L1  8-2
1                Utah Jazz  10   4  0.714  0.5  3-2  7-2  3-1   6-2  111.1    105.1   6.0   W6  8-2
2              LA Clippers  10   4  0.714  0.5  5-2  5-2  4-1   8-4  114.9    108.6   6.3   W4  7-3
3           Boston Celtics   8   4  0.667  1.5  4-2  4-2  1-2   7-4  110.8    109.5   1.3   L1  7-3
4          Milwaukee Bucks   9   5  0.643  1.5  6-1  3-4  5-0   7-4  120.4    110.6   9.8   L1  7-3
5       Philadelphia 76ers   9   5  0.643  1.5  7-1  2-4  2-1   9-3  113.6    110.4   3.2   L1  6-4
6           Indiana Pacers   8   5  0.615    2  4-3  4-2  2-0   4-2  112.9    109.8   3.1   L1  5-5
7            Brooklyn Nets   9   6  0.600    2  7-4  2-2  3-0   6-3  119.3    113.7   5.6   W4  6-4
8             Phoenix Suns   7   5  0.583  2.5  3-1  4-4  1-2   5-3  109.9    106.8   3.1   L2  6-4
9        San Antonio Spurs   8   6  0.571  2.5  2-4  6-2  2-2   7-6  111.1    109.8   1.3   W2  6-4
10  Portland Trail Blazers   8   6  0.571  2.5  4-4  4-2  1-1   6-4  114.9    115.9  -1.0   L1  6-4
11   Golden State Warriors   7   6  0.538    3  4-3  3-3  3-1   4-3  111.6    115.2  -3.6   W1  6-4
12       Memphis Grizzlies   7   6  0.538    3  3-5  4-1  0-1   2-3  106.6    107.3  -0.7   W5  6-4
13          Denver Nuggets   7   7  0.500  3.5  4-5  3-2  3-1   5-6  115.6    111.7   3.9   W1  6-4
14         New York Knicks   7   8  0.467    4  3-4  4-4  1-3   6-6  100.6    102.7  -2.1   W2  5-5
15     Cleveland Cavaliers   6   7  0.462    4  3-3  3-4  1-2   5-5   99.5    103.9  -4.4   W1  3-7
16           Atlanta Hawks   6   7  0.462    4  3-3  3-4  0-2   4-5  111.2    109.2   2.0   W1  3-7
17        Dallas Mavericks   6   7  0.462    4  2-2  4-5  1-0   3-2  107.5    106.8   0.7   L3  5-5
18   Oklahoma City Thunder   6   7  0.462    4  1-5  5-2  0-2   1-5  105.6    112.6  -7.0   L1  5-5
19           Orlando Magic   6   8  0.429  4.5  3-3  3-5  3-0   5-5  104.5    110.7  -6.2   L6  2-8
20       Charlotte Hornets   6   8  0.429  4.5  3-3  3-5  2-0   4-5  107.6    108.1  -0.5   L3  4-6
21           Chicago Bulls   6   8  0.429  4.5  2-3  4-5  0-2   2-3  117.4    120.0  -2.6   W2  5-5
22              Miami Heat   5   7  0.417  4.5  4-3  1-4  1-1   3-6  110.3    113.8  -3.5   W1  4-6
23    New Orleans Pelicans   5   8  0.385    5  2-3  3-5  1-0   3-5  107.3    109.5  -2.2   L1  3-7
24         Toronto Raptors   5   8  0.385    5  4-2  1-6  1-2   3-2  111.8    110.7   1.1   W3  5-5
25        Sacramento Kings   5   9  0.357  5.5  4-6  1-3  1-3   3-8  114.3    123.6  -9.3   L3  2-8
26         Houston Rockets   4   8  0.333  5.5  3-3  1-5  1-2   3-6  110.2    112.7  -2.5   L2  4-6
27      Washington Wizards   3   8  0.273    6  1-5  2-3  0-3   1-8  120.5    121.3  -0.8   W1  3-7
28  Minnesota Timberwolves   3   9  0.250  6.5  2-4  1-5  1-3   2-7  107.7    117.8 -10.1   L2  2-8
29         Detroit Pistons   3  10  0.231    7  2-5  1-5  0-4   2-7  108.9    113.6  -4.7   L1  3-7
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd

# %% Types
result: pd.DataFrame

# %% Data
DATA = 'https://python3.info/_static/espn-standing.html'
# DATA = 'https://www.espn.com/nba/standings/_/group/league'

# %% Result
result = ...