赞
踩
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['axes.unicode_minus'] = False
sns.set_style('darkgrid', {'font.sans-serif':['SimHei', 'Arial']})
df_player = pd.read_csv(r"C:\Users\imqqdong\Downloads\NBAdata\season_2021_detailed.csv")
df_team = pd.read_csv(r"C:\Users\imqqdong\Downloads\NBAdata\season_2021_basic.csv")
df_team.head()
date | weekday | home_team | home_score | away_team | away_score | attendance | overtime | remarks | |
---|---|---|---|---|---|---|---|---|---|
0 | 2020-12-22T19:00:00 | Tuesday | Brooklyn Nets | 125 | Golden State Warriors | 99 | 0 | NaN | NaN |
1 | 2020-12-22T22:00:00 | Tuesday | Los Angeles Lakers | 109 | Los Angeles Clippers | 116 | 0 | NaN | NaN |
2 | 2020-12-23T19:00:00 | Wednesday | Cleveland Cavaliers | 121 | Charlotte Hornets | 114 | 0 | NaN | NaN |
3 | 2020-12-23T19:00:00 | Wednesday | Indiana Pacers | 121 | New York Knicks | 107 | 0 | NaN | NaN |
4 | 2020-12-23T19:00:00 | Wednesday | Orlando Magic | 113 | Miami Heat | 107 | 0 | NaN | NaN |
df_team_sim = df_team.drop(columns=["weekday","attendance","overtime","remarks"])
df_team_sim["winner"] = 0
df_team_sim["loser"] = 0
for i in range(len(df_team_sim)):
if df_team_sim["home_score"][i] > df_team_sim["away_score"][i]:
df_team_sim["winner"][i] = df_team_sim["home_team"][i]
df_team_sim["loser"][i] = df_team_sim["away_team"][i]
elif df_team_sim["home_score"][i] < df_team_sim["away_score"][i]:
df_team_sim["winner"][i] = df_team_sim["away_team"][i]
df_team_sim["loser"][i] = df_team_sim["home_team"][i]
sns.countplot(x = df_team_sim["winner"],color = "orange")
plt.xticks(rotation = 90)
plt.show()
win_lose_times = pd.DataFrame(df_team_sim["winner"].value_counts())
win_lose_times["lose_times"] = df_team_sim["loser"].value_counts()
#会不会自动对齐索引
win_lose_times.head()
winner | lose_times | |
---|---|---|
Phoenix Suns | 63 | 25 |
Utah Jazz | 58 | 25 |
Los Angeles Clippers | 57 | 34 |
Milwaukee Bucks | 57 | 31 |
Philadelphia 76ers | 56 | 28 |
win_lose_times.rename(columns={"winner":"win_times"},inplace = True)
win_lose_times["wp"] = win_lose_times["win_times"]/(win_lose_times["win_times"]+win_lose_times["lose_times"])
win_lose_times.sort_values(by = "wp",ascending=False)
win_times | lose_times | wp | |
---|---|---|---|
Phoenix Suns | 63 | 25 | 0.715909 |
Utah Jazz | 58 | 25 | 0.698795 |
Philadelphia 76ers | 56 | 28 | 0.666667 |
Brooklyn Nets | 55 | 29 | 0.654762 |
Milwaukee Bucks | 57 | 31 | 0.647727 |
Los Angeles Clippers | 57 | 34 | 0.626374 |
Denver Nuggets | 51 | 31 | 0.621951 |
Atlanta Hawks | 51 | 38 | 0.573034 |
Dallas Mavericks | 45 | 34 | 0.569620 |
Los Angeles Lakers | 45 | 34 | 0.569620 |
Portland Trail Blazers | 44 | 34 | 0.564103 |
New York Knicks | 42 | 35 | 0.545455 |
Golden State Warriors | 39 | 35 | 0.527027 |
Miami Heat | 40 | 36 | 0.526316 |
Memphis Grizzlies | 41 | 38 | 0.518987 |
Boston Celtics | 38 | 40 | 0.487179 |
Indiana Pacers | 35 | 39 | 0.472973 |
Washington Wizards | 36 | 43 | 0.455696 |
Charlotte Hornets | 33 | 40 | 0.452055 |
San Antonio Spurs | 33 | 40 | 0.452055 |
Sacramento Kings | 31 | 41 | 0.430556 |
New Orleans Pelicans | 31 | 41 | 0.430556 |
Chicago Bulls | 31 | 41 | 0.430556 |
Toronto Raptors | 27 | 45 | 0.375000 |
Minnesota Timberwolves | 23 | 49 | 0.319444 |
Cleveland Cavaliers | 22 | 50 | 0.305556 |
Oklahoma City Thunder | 22 | 50 | 0.305556 |
Orlando Magic | 21 | 51 | 0.291667 |
Detroit Pistons | 20 | 52 | 0.277778 |
Houston Rockets | 17 | 55 | 0.236111 |
fig = plt.figure(figsize = (30,20),dpi =100)
x = np.arange(len(win_lose_times.index.values))
width = 0.4
plt.bar(x = x - width/2,height = win_lose_times["win_times"],label = "胜利场次",align="center",width = width)
plt.bar(x = x + width/2,height = win_lose_times["lose_times"],label = "失败场次",align="center",width = width)
plt.xticks(ticks = x,rotation = 90,fontsize =25,labels = win_lose_times.index.values)
plt.yticks(fontsize = 25)
plt.legend(fontsize = 25)
plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。