pandas分析NBA-赛季球员球队数据
进入NBA中国官方网站:/playerindex/
通过浏览器操作检查 -> Network -> F5 双击json的文件进入
链接地址/static/data/league/playerlist.json
现在的格式显示不利于查看,请安装JSONview的扩展程序
代码编写球员信息抓取
playerinfo.py
# -*-coding:utf-8-*-import requestsimport pandas as pduser_agent = 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)'headers = {'User-Agent':user_agent}url = '/static/data/league/playerlist.json'# 解析网页r = requests.get(url,headers=headers).json()num = int(len(r['payload']['players']))-1 #得到列表r['payload']['players']的长度print "长度为", nump1_cols = []# 用来存放p1数组的列p2_cols = []# 用来存放p2数组的列# 遍历其中一个['playerProfile'],['teamProfile'] 得到各自列名,添加到p1_cols和p2_cols列表中for x in r['payload']['players'][0]['playerProfile']:p1_cols.append(x)for y in r['payload']['players'][0]['teamProfile']:p2_cols.append(y)p1 = pd.DataFrame(columns=p1_cols)# 初始化一个DataFrame p1 用来存放playerProfile下的数据p2 = pd.DataFrame(columns=p2_cols)# 初始化一个DataFrame p1 用来存放playerProfile下的数据# 遍历一次得到一个球员的信息,分别添加到DataFrame数组中for z in range(num):player = pd.DataFrame([r['payload']['players'][z]['playerProfile']])team = pd.DataFrame([r['payload']['players'][z]['teamProfile']])p1 = p1.append(player, ignore_index=True)p2 = p2.append(team, ignore_index=True)p3 = pd.merge(p1, p2, left_index=True, right_index=True)print p3# 数据合并p3.to_csv('nba_player.csv', index=False, encoding="utf-8")# 保存文件
代码编写球员数据抓取
playerstatic.py
# -*-coding:utf-8-*-import requestsimport pandas as pduser_agent = 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)'headers = {'User-Agent': user_agent}url = '/static/data/league/playerstats_All_All_All_0_All_false__2_All_Team_points_All_perGame.json'# 解析网页r = requests.get(url,headers=headers).json()num = int(len(r['payload']['players']))-1 #得到列表r['payload']['players']的长度print "长度为", nump1_cols = []# 存放球员的信息p2_cols = []# 存放球员所在球队的新消息p3_cols = []# 存放球员平均数据p4_cols = []# 存放球员数据# 遍历其中一个['playerProfile'],['teamProfile'] 得到各自列名,添加到p1_cols和p2_cols列表中for x in r['payload']['players'][0]['playerProfile']:p1_cols.append(x)for x in r['payload']['players'][0]['teamProfile']:p2_cols.append(x)for x in r['payload']['players'][0]['statAverage']:p3_cols.append(x)for x in r['payload']['players'][0]['statTotal']:p4_cols.append(x)# 初始化一个DataFrame 用来存放数据p1 = pd.DataFrame(columns=p1_cols)p2 = pd.DataFrame(columns=p2_cols)p3 = pd.DataFrame(columns=p3_cols)p4 = pd.DataFrame(columns=p4_cols)# 遍历一次得到一个球员的信息,分别添加到DataFrame数组中for z in range(num):player = pd.DataFrame([r['payload']['players'][z]['playerProfile']])team = pd.DataFrame([r['payload']['players'][z]['teamProfile']])statAverage = pd.DataFrame([r['payload']['players'][z]['statAverage']])statTotal = pd.DataFrame([r['payload']['players'][z]['statTotal']])p1 = p1.append(player, ignore_index=True)p2 = p2.append(team, ignore_index=True)p3 = p3.append(statAverage, ignore_index=True)p4 = p4.append(statTotal, ignore_index=True)# 数据合并 两两合并p6 = pd.merge(p1, p2, left_index=True, right_index=True)p7 = pd.merge(p3, p4, left_index=True, right_index=True)p5 = pd.merge(p6, p7, left_index=True, right_index=True)#print p5# 保存文件p5.to_csv('nba_player_static.csv', index=False, encoding="utf-8")
数据整合
将数据读入# -*-coding:utf-8-*-import numpy as npimport pandas as pdimport matplotlib.pyplot as pltplayer = pd.read_csv("nba_player.csv") # 读入球员基本信息playerstatic = pd.read_csv("nba_player_static.csv") # 读入球员的数据信息
球员头信息尾信息
player.head()
前五条记录
player.tail()
后五条记录
球员各个国家统计
player['country'].value.counts()
喀麦隆~~~~恩比德大帝
各大洲球员统计
NorthAmerica = {'巴哈马','美国','加拿大','海地','多米尼加共和国','波多黎各'} SouthAmerica = {'哥伦比亚','委内瑞拉','圭亚那','苏里南','厄瓜多尔','秘鲁','巴西','玻利维亚','智利','巴拉圭','乌拉圭','阿根廷'}Europe = {'法国','西班牙','德国','瑞典','黑山', '波兰','捷克共和国', '斯洛文尼亚乌克兰', '奥地利', '希腊' ,'芬兰', '英国', '俄罗斯', '波斯尼亚和黑塞哥维那', '塞尔维亚', '克罗地亚','波斯尼亚' ,'意大利','瑞士', '立陶宛', '比利时', '拉脱维亚' }Africa = {'喀麦隆','南苏丹','突尼斯','刚果民主共和国', '苏丹', '刚果','塞内加尔','加纳','马里', '埃及'}Oceania = {'澳洲','新西兰'}Asia = {'土耳其', '中国', '以色列', '格鲁吉亚'}asiaPlayer = player[player['country'].isin(Asia)]northAmericaPlayer = player[player['country'].isin(NorthAmerica)]southAmericaPlayer = player[player['country'].isin(SouthAmerica)]europePlayer = player[player['country'].isin(Europe)]oceaniaPlayer = player[player['country'].isin(Oceania)]africaPlayer = player[player['country'].isin(Africa)]# 各大洲球员人数north = len(northAmericaPlayer)south = len(southAmericaPlayer)europe = len(europePlayer)africa = len(africaPlayer)asia = len(asiaPlayer)oceania = len(oceaniaPlayer)d=[north,south,europe,africa,asia,oceania]i=['north','south','europe','africa','asia','oceania']n=[0,1,2,3,4,5]contient = pd.Series(data=d,index=i)print '北美洲球员人数:',northprint '南美洲球员人数:',southprint '欧洲球员人数:',europeprint '非洲球员人数:',africaprint '亚洲球员人数:',asiaprint '大洋洲洲球员人数:',oceania# 各大洲球员人数统计图contient.sort_index(ascending=True)contient.plot(kind='bar',alpha = 0.5)plt.xlabel("contient")plt.ylabel("theNumOfPlayer")plt.title("The number of players on all continents ")for a,b in zip(n,d): # matplotlib可视化之如何给图形添加数据标签plt.text(a-0.1, b+0.05, '%.0f' % b)plt.show()
各个位置球员人数统计
player['position'].value_counts()
小球时代,传统中锋所剩无几
将球员按照选秀时间直方图
df = playerdf = df[(True-df['draftYear'].isin([00]))] ##删除掉 00年球员 不必要的干扰行数据df = df['draftYear'].value_counts() # 统计各个年份球员人数df.index.name='Year' # 为数据设置索引值df = df.sort_index(ascending=True) # 按照降序## 绘制条形统计图df.plot(kind = 'bar', alpha = 0.5)plt.xlabel('years')plt.ylabel('count')plt.title('- NBA season players')plt.show()
就剩卡特一人了计算现役球员平均职业年龄
career_age = player['experience'].value_counts()#职业年龄统计career_age.values # 各职业时间对应人数career_age.index # 各职业时间k = career_age.index * career_age.values # 各职业时间*各职业时间对应人数k = pd.Series(k) #转换数据类型averAge = (k.sum()*1.0)/career_age.sum()
输出结果为 4.2931726907630523 年
知识点
Series 的sort_index(ascending=True)方法可以为对index进行排序操作True表示升序操作,False表示降序操作若要按值对 Series 进行排序,当使用 .order(na_last=True, ascending=True, kind=’mergesort’) 方法,任何缺失值默认都会被放到 Series 的末尾。在DataFrame上,.sort_index(axis=0, by=None, ascending=True) 方法多了一个轴向的选择参数与一个 by 参数的作用是针对某一(些)列进行排序(不能对行使用 by 参数)。注意在使用sort_index对DataFrame进行排序的时候,不能直接对index和columns都含有的字段进行排序,会报错。isin()将要过滤的数据放入括号内 且以字典的形式len()用来统计DataFrame的行数matplotlib可视化之如何给图形添加数据标签?
如果觉得《pandas分析NBA-赛季球员球队数据》对你有帮助,请点赞、收藏,并留下你的观点哦!