当前位置:   article > 正文

2020赛季NBA各队胜负场展示——python数据处理与可视化_利用pandas与相关可视化库完成体育类数据的分析、处理与可视化任务

利用pandas与相关可视化库完成体育类数据的分析、处理与可视化任务
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']})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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")
  • 1
  • 2
df_team.head()
  • 1
dateweekdayhome_teamhome_scoreaway_teamaway_scoreattendanceovertimeremarks
02020-12-22T19:00:00TuesdayBrooklyn Nets125Golden State Warriors990NaNNaN
12020-12-22T22:00:00TuesdayLos Angeles Lakers109Los Angeles Clippers1160NaNNaN
22020-12-23T19:00:00WednesdayCleveland Cavaliers121Charlotte Hornets1140NaNNaN
32020-12-23T19:00:00WednesdayIndiana Pacers121New York Knicks1070NaNNaN
42020-12-23T19:00:00WednesdayOrlando Magic113Miami Heat1070NaNNaN
df_team_sim = df_team.drop(columns=["weekday","attendance","overtime","remarks"])
  • 1
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]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
sns.countplot(x = df_team_sim["winner"],color = "orange")
plt.xticks(rotation = 90)
plt.show() 
  • 1
  • 2
  • 3

在这里插入图片描述

win_lose_times = pd.DataFrame(df_team_sim["winner"].value_counts())
  • 1
win_lose_times["lose_times"] = df_team_sim["loser"].value_counts()
#会不会自动对齐索引
  • 1
  • 2
win_lose_times.head()
  • 1
winnerlose_times
Phoenix Suns6325
Utah Jazz5825
Los Angeles Clippers5734
Milwaukee Bucks5731
Philadelphia 76ers5628
win_lose_times.rename(columns={"winner":"win_times"},inplace = True)
  • 1
win_lose_times["wp"] = win_lose_times["win_times"]/(win_lose_times["win_times"]+win_lose_times["lose_times"])
  • 1
win_lose_times.sort_values(by = "wp",ascending=False)
  • 1
win_timeslose_timeswp
Phoenix Suns63250.715909
Utah Jazz58250.698795
Philadelphia 76ers56280.666667
Brooklyn Nets55290.654762
Milwaukee Bucks57310.647727
Los Angeles Clippers57340.626374
Denver Nuggets51310.621951
Atlanta Hawks51380.573034
Dallas Mavericks45340.569620
Los Angeles Lakers45340.569620
Portland Trail Blazers44340.564103
New York Knicks42350.545455
Golden State Warriors39350.527027
Miami Heat40360.526316
Memphis Grizzlies41380.518987
Boston Celtics38400.487179
Indiana Pacers35390.472973
Washington Wizards36430.455696
Charlotte Hornets33400.452055
San Antonio Spurs33400.452055
Sacramento Kings31410.430556
New Orleans Pelicans31410.430556
Chicago Bulls31410.430556
Toronto Raptors27450.375000
Minnesota Timberwolves23490.319444
Cleveland Cavaliers22500.305556
Oklahoma City Thunder22500.305556
Orlando Magic21510.291667
Detroit Pistons20520.277778
Houston Rockets17550.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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/753870
推荐阅读
相关标签
  

闽ICP备14008679号