Reputation: 31
I am analyzing some NBA stats. After scraping a website, I extract information such as the Home Team, Away Team, Points etc... and print them.
I would like to compute the difference between the points made by Home Team and Away Team, but since the points are of type string
, I first need to cast them to int
.
I tried int(away_points)
, float(away_points)
and int(float(away_points))
, with no luck.
The error that I receive is ValueError: invalid literal for int() with base 10: ''
.
However, running the code below shows numerical values for the points:
Away Team: Golden State Warriors
Home Team: Brooklyn Nets
Away points: 99
Home points: 125
<class 'str'>
So I do not really know where the error comes from. Any help is very much appreciated.
for row in rows:
if('data-stat="visitor_team_name"'):
away_team = str(row).partition('data-stat="visitor_team_name">')[2].partition("</a")[0].partition('.html">')[2]
print(f"Away Team: {away_team}")
if('data-stat="home_team_name"'):
home_team = str(row).partition('data-stat="home_team_name">')[2].partition("</a")[0].partition('.html">')[2]
print(f"Home Team: {home_team}")
if('data-stat="visitor_pts"'):
away_pts = str(row).partition('data-stat="visitor_pts">')[2].partition("<")[0]
print(f"Away points: {away_pts}")
if('data-stat="home_pts"'):
home_pts = str(row).partition('data-stat="home_pts">')[2].partition("<")[0]
print(f"Home points: {home_pts}")
print(type(away_pts))
# Here I would like to convert to integers the points e.g., int(away_pts), to get the points difference
if(away_pts > home_pts):
print(away_pts - home_pts)
winner = away_team
else:
winner = home_team
Upvotes: 1
Views: 268
Reputation: 28595
Reading in the table with pandas, it automatically converts the PTS columns to int.
import pandas as pd
url = 'https://www.basketball-reference.com/leagues/NBA_2021_games-december.html'
df = pd.read_html(url)[0]
for idx, row in df.iterrows():
away_team = row['Visitor/Neutral']
away_pts = row['PTS']
home_team = row['Home/Neutral']
home_pts = row['PTS.1']
diff = (away_pts - home_pts)
if(away_pts > home_pts):
winner = away_team
else:
diff = diff*-1
winner = home_team
print(f"\nAway Team: {away_team}")
print(f"Home Team: {home_team}")
print(f"Away points: {away_pts}")
print(f"Home points: {home_pts}")
print(f"{winner} won by {diff} points.\n")
**Output:
...
Away Team: Chicago Bulls
Home Team: Washington Wizards
Away points: 133
Home points: 130
Chicago Bulls won by 3 points.
Away Team: Philadelphia 76ers
Home Team: Orlando Magic
Away points: 116
Home points: 92
Philadelphia 76ers won by 24 points.
Away Team: Sacramento Kings
Home Team: Houston Rockets
Away points: 119
Home points: 122
Houston Rockets won by 3 points.
Away Team: New York Knicks
Home Team: Toronto Raptors
Away points: 83
Home points: 100
Toronto Raptors won by 17 points.
Away Team: New Orleans Pelicans
Home Team: Oklahoma City Thunder
Away points: 113
Home points: 80
New Orleans Pelicans won by 33 points.
Away Team: Phoenix Suns
Home Team: Utah Jazz
Away points: 106
Home points: 95
Phoenix Suns won by 11 points.
Upvotes: 1