import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style("ticks")#设置绘图风格
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
path='athlete_events.csv'
data=pd.read_csv(path)
奥运会举办了多少届?
#提取出历届奥运举办城市并去重
games=data.loc[:,['Year','Season','City']].drop_duplicates()
games=games.sort_values(by=['Season','Year'])
games=games.set_index(['Season'])
summer_games=games.loc['Summer',:]
winter_games=games.loc['Winter',:]
print("从{}年到{}年,共举办了{}届奥运会。".format(games['Year'].min(),games['Year'].max(),len(games)))
print("夏季奥运会从{}年开始举办,共举办了{}届。".format(summer_games['Year'].min(),len(summer_games)))
print("冬季奥运会从{}年开始举办,共举办了{}届。".format(winter_games['Year'].min(),len(winter_games)))
从1896年到2016年,共举办了52届奥运会。
夏季奥运会从1896年开始举办,共举办了30届。
冬季奥运会从1924年开始举办,共举办了22届。
有多少个城市举办过奥运会?
games_city=games['City'].value_counts()
summer_games_city=summer_games['City'].value_counts()
winter_games_city=winter_games['City'].value_counts()
print("有{}个城市举办过奥运会,其中{}个举办过夏季奥运会,{}个举办过冬季奥运会,没有城市同时举办过夏季奥运会和冬季奥运会。".format(len(games_city),len(summer_games_city),len(winter_games_city)))
有42个城市举办过奥运会,其中23个举办过夏季奥运会,19个举办过冬季奥运会,没有城市同时举办过夏季奥运会和冬季奥运会。
print("举办过两次或两次以上夏季奥运会的城市:")
for c,t in zip(summer_games_city[summer_games_city>1].index,summer_games_city[summer_games_city>1].values):
print("{}:{}次".format(c,t))
print("\n举办过两次或两次以上冬季奥运会的城市:")
for c,t in zip(winter_games_city[winter_games_city>1].index,winter_games_city[winter_games_city>1].values):
print("{}:{}次".format(c,t))
举办过两次或两次以上夏季奥运会的城市:
Athina:3次
London:3次
Paris:2次
Los Angeles:2次
Stockholm:2次
举办过两次或两次以上冬季奥运会的城市:
Sankt Moritz:2次
Lake Placid:2次
Innsbruck:2次
奥运会有多少运动项目?
#项目去重
games_sports=data.loc[:,['Year','Season','Sport','Event']].drop_duplicates()
summer_sports=games_sports[games_sports['Season']=='Summer'].drop_duplicates()
winter_sports=games_sports[games_sports['Season']=='Winter'].drop_duplicates()
#汇总夏季、冬季奥运会的运动项目
summer_sports_counts=summer_sports.groupby(['Year','Sport']).count()
del summer_sports_counts['Season']
winter_sports_counts=winter_sports.groupby(['Year','Sport']).count()
del winter_sports_counts['Season']
fig=plt.figure(figsize=(12,6),dpi=100)
ax1=fig.add_subplot(111)
ax2=ax1.twinx()#建立副纵坐标轴
data_1=summer_sports_counts.groupby('Year').count().reset_index().sort_index()
data_2=summer_sports_counts.groupby('Year').sum().reset_index().sort_index()
x=np.arange(len(data_1))
y1=data_1['Event']
y2=data_2['Event']
tick_label=data_1['Year']
ax1.bar(x,y1,color='#eea2a4',label='运动大项')
ax1.set_xticks([i for i in range(len(data_1))])#设置x轴刻度,注意起始是1还是0
ax1.set_xticklabels(tick_label)
ax1.set_xlim(-1,len(data_1))
ax1.set_title("夏季奥运会运动项目数量变迁",fontsize=18)
ax2.plot(x,y2,marker='.',linewidth=0.8,color='#ed5a65',label='比赛项目')#将比赛项目数绘制在副坐标上
ax1.set_ylabel('运动大项数量')
ax2.set_ylabel('比赛项目数量')
ax1.legend(loc=(0.01,0.92))
ax2.legend(loc=(0.01,0.86))
fig=plt.figure(figsize=(12,6),dpi=100)
ax1=fig.add_subplot(111)
ax2=ax1.twinx()
data_1=winter_sports_counts.groupby('Year').count().reset_index().sort_index()
data_2=winter_sports_counts.groupby('Year').sum().reset_index().sort_index()
x=np.arange(len(data_1))
y1=data_1['Event']
y2=data_2['Event']
tick_label=data_1['Year']
ax1.bar(x,y1,color='#c3d7df',label='运动大项')
ax1.set_xticks([i for i in range(len(data_1))])#设置x轴刻度,注意起始是1还是0
ax1.set_xticklabels(tick_label)
ax1.set_xlim(-1,len(data_1))
ax1.set_title("冬季奥运会运动大项数量变迁",fontsize=18)
ax2.plot(x,y2,marker='.',linewidth=0.8,color='#5cb3cc',label='比赛项目')
ax1.set_ylabel('运动大项数量')
ax2.set_ylabel('比赛项目数量')
ax1.legend(loc=(0.01,0.92))
ax2.legend(loc=(0.01,0.86))
从整体上看,奥运会的运动大项和比赛项目数量都是逐年增加的。
运动大项数量方面,2000年以后夏季奥运会和冬季奥运会的运动大项数量基本保持不变了。
比赛项目方面,随着运动大项数量的逐渐稳定,夏季奥运会的比赛项目数量也逐渐稳定,但冬季奥运会的比赛项目数量还在逐年增加。
fig=plt.figure(figsize=(10,14),dpi=100)
ax1=fig.add_subplot(211)
data_1=summer_sports_counts.loc[2016].sort_values(by='Event',ascending=False)
x1=np.arange(len(data_1))
y1=data_1['Event']
y1_tick_label=data_1.index
ax1.barh(x1,y1,color='#ed5a65')
ax1.set_yticks([i for i in range(len(data_1))])#设置y轴刻度,注意起始是1还是0
ax1.set_yticklabels(y1_tick_label)
ax1.get_xaxis().set_visible(False)#设置x轴不可见
ax1.set_title("2016年里约奥运会运动小项数量",fontsize=18)
for x,y in enumerate(y1):
ax1.text(y+0.5,x,y,va='center')
ax2=fig.add_subplot(212)
data_2=winter_sports_counts.loc[2014].sort_values(by='Event',ascending=False)
x2=np.arange(len(data_2))
y2=data_2['Event']
y2_tick_label=data_2.index
ax2.barh(x2,y2,color='#c3d7df')
ax2.set_yticks([i for i in range(len(data_2))])#设置y轴刻度,注意起始是1还是0
ax2.set_yticklabels(y2_tick_label)
ax2.get_xaxis().set_visible(False)#设置x轴不可见
ax2.set_title("2014年索契冬奥会运动小项数量",fontsize=18)
for x,y in enumerate(y2):
ax2.text(y+0.1,x,y,va='center')
夏季奥运会不同运动项目的比赛项目数量差异较大。2016年里约奥运会,比赛项目最多的两个运动项目是田径(47项)和游泳(34项),远多于其他运动。
而冬季奥运会的比赛项目数量差异相对较小。2014年索契冬奥会比赛项目最多的两项是越野滑雪(12项)和速度滑冰(12项)。
有多少个国家(地区)参加过奥运会?
country=data.loc[:,['NOC','Year','Season']].drop_duplicates()
country_summer=country[country['Season']=='Summer'].drop(['Season'],axis=1)
country_winter=country[country['Season']=='Winter'].drop(['Season'],axis=1)
country_summer=country_summer.groupby('Year').count().reset_index()
country_winter=country_winter.groupby('Year').count().reset_index()
fig=plt.figure(figsize=(12,8),dpi=100)
ax1=fig.add_subplot(121)
x1=np.arange(len(country_summer))
y1=country_summer.sort_index(ascending=False)['NOC']
tick_label=country_summer.sort_index(ascending=False)['Year']
ax1.barh(x1,y1,color='#ed5a65')
ax1.set_yticks([i for i in x1])#设置y轴刻度,注意起始是1还是0
ax1.set_yticklabels(tick_label)
ax1.set_ylim(-1,len(country_summer))
ax1.set_xlim(0,220)
ax1.get_xaxis().set_visible(False)#设置x轴不可见
ax1.set_title("历届夏季奥运会参加国家(地区)")
for x,y in enumerate(y1):
ax1.text(y+0.5,x,y,va='center')
ax2=fig.add_subplot(122)
#data_2=winter_sports_counts.groupby('Year').count().reset_index().sort_index(ascending=False)
x2=np.arange(len(country_winter))
y2=country_winter.sort_index(ascending=False)['NOC']
tick_label=country_winter.sort_index(ascending=False)['Year']
ax2.barh(x2,y2,color='#c3d7df')
ax2.set_yticks([i for i in x2])#设置y轴刻度,注意起始是1还是0
ax2.set_yticklabels(tick_label)
ax2.set_ylim(-1,len(country_winter))
ax2.get_xaxis().set_visible(False)#设置x轴不可见
ax2.set_title("历届冬季奥运会参加国家(地区)")
for x,y in enumerate(y2):
ax2.text(y+0.5,x,y,va='center')
奥运会的参赛国家(地区)是逐年增加的,除了1976年蒙特利尔奥运会和1980莫斯科奥运会。
1976年是蒙特利尔奥运会是由于南非政权邀请新西兰橄榄球队访问,坦桑尼亚等22个非洲国家联合抵制新西兰参加的当年奥运会。
1980莫斯科奥运会则是由于1979年圣诞节前夕,前苏联出兵入侵阿富汗,因此遭到了许多国家的抵制。
关于参赛运动员
ID是运动员的唯一标识,一个运动员可能参加好几届奥运会。因此,若要统计历届奥运会的相关信息,就不能单独利用ID去重。
#整理运动员数据,若一个运动员在一届奥运会中参加多个项目,那么只保留一次
athlete=data.loc[:,['ID','Name','Sex','Age','Height','Weight','Team','NOC','Year','Season','City','Sport','Event']].drop_duplicates(subset=['ID','Year'])
athlete_summer=athlete[athlete['Season']=='Summer']
athlete_winter=athlete[athlete['Season']=='Winter']
运动员的性别
历届奥运的性别比
athlete_sex=athlete.loc[:,['Year','Sex','Season']]
#利用crosstab制作性别交叉表,分年统计男女人数
athlete_sex_count=pd.crosstab(athlete_sex['Year'],athlete_sex['Sex'],margins=True).reset_index()
fig=plt.figure(figsize=(6,6),dpi=100)
ax=fig.add_subplot(111)
size=0.5
labels=['Female','Male']
ax.pie(athlete_sex_count.iloc[-1,[1,2]],
labels=labels,startangle=90,autopct='%.1f%%',colors=sns.color_palette("husl",2),
radius=1.1,#控制饼图半径,默认为1
pctdistance=0.75,#控制百分比显示位置
counterclock=False,#控制显示顺序,默认逆时针
wedgeprops=dict(width=size,edgecolor='w'),#控制甜甜圈的宽度,边缘颜色等
textprops=dict(fontsize=10)#控制字号及颜色等
)
ax.set_title('奥运会运动员男女性别比',fontsize=15)
总体来看,参加过奥运会的男运动员远多于女运动员,男运动员比例近四分之三。
#统计夏季奥运性别
summer_sex=athlete.loc[:,['Year','Sex','Season']][athlete.loc[:,['Year','Sex','Season']]['Season']=='Summer']
summer_sex_count=pd.crosstab(summer_sex['Year'],summer_sex['Sex']).reset_index()
summer_sex_count['Sex_ratio']=summer_sex_count['F']/summer_sex_count['M']
#统计冬季奥运性别
winter_sex=athlete.loc[:,['Year','Sex','Season']][athlete.loc[:,['Year','Sex','Season']]['Season']=='Winter']
winter_sex_count=pd.crosstab(winter_sex['Year'],winter_sex['Sex']).reset_index()
winter_sex_count['Sex_ratio']=winter_sex_count['F']/winter_sex_count['M']
fig,ax=plt.subplots(figsize=(12,6),dpi=100)
x1=summer_sex_count['Year']
y1=summer_sex_count['Sex_ratio']
x2=winter_sex_count['Year']
y2=winter_sex_count['Sex_ratio']
ax.plot(x1,y1,marker='o',color='#ed5a65',label='Summer Olympic')
ax.plot(x2,y2,marker='^',color='#c3d7df',label='Winter Olympic')
ax.set_xticks(np.linspace(1896,2016,num=31))
ax.set_ylabel('性别比(女/男)')
ax.set_title('历届奥运会男女性别比',fontsize=18)
ax.legend()
整体上看,奥运会女性运动员的比例是逐渐升高的。第一届奥运会没有女运动员参加。
到80年代,女性运动员比例升高得比较缓慢。
从80年代中期开始,女运动员比例升高得非常迅速。到2016年,夏季奥运会的运动员男女性别比(女/男)已经超过0.8。近几届冬季奥运会的女运动员比例低于夏季奥运会,运动员男女性别比(女/男)为0.7左右。
运动项目的性别比
#按运动分类计算运动员性别比
sport_sex=athlete.loc[:,['Sport','Sex','Season']]
sport_sex_count=pd.crosstab(sport_sex['Sport'],sport_sex['Sex']).reset_index()
sport_sex_count['Sex_ratio']=sport_sex_count['F']/sport_sex_count['M']
sport_sex_count=sport_sex_count.sort_values(by='Sex_ratio',ascending=False)
sport_sex_count[(sport_sex_count['F']==0)|(sport_sex_count['M']==0)]
Sex | Sport | F | M | Sex_ratio |
---|---|---|---|---|
41 | Rhythmic Gymnastics | 658 | 0 | inf |
52 | Softball | 478 | 0 | inf |
55 | Synchronized Swimming | 728 | 0 | inf |
61 | Tug-Of-War | 0 | 170 | 0.000000 |
0 | Aeronautics | 0 | 1 | 0.000000 |
44 | Rugby | 0 | 159 | 0.000000 |
42 | Roque | 0 | 4 | 0.000000 |
40 | Racquets | 0 | 5 | 0.000000 |
39 | Polo | 0 | 94 | 0.000000 |
38 | Nordic Combined | 0 | 720 | 0.000000 |
35 | Military Ski Patrol | 0 | 23 | 0.000000 |
31 | Jeu De Paume | 0 | 11 | 0.000000 |
15 | Cricket | 0 | 24 | 0.000000 |
9 | Basque Pelota | 0 | 2 | 0.000000 |
7 | Baseball | 0 | 894 | 0.000000 |
33 | Lacrosse | 0 | 60 | 0.000000 |
艺术体操、垒球、花样游泳只有女运动员参加。
棒球、橄榄球等13项运动只有男运动员参加。
sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)]
fig=plt.figure(figsize=(10,15),dpi=100)
ax=fig.add_subplot(111)
x=range(len(sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)]))
y=sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)]['Sex_ratio']
tick_label=sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)]['Sport']
ax.barh(x,y,color='#ff9900')
ax.set_yticks([i for i in range(len(sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)]))])
ax.set_yticklabels(tick_label)
ax.set_ylim(-1,50)
ax.set_xlabel('性别比(女/男)')
ax.set_title('奥运会各项运动的男女性别比',fontsize=18)
for x,y in enumerate(y):
ax.text(y+0.003,x,'{:.3f}'.format(y),va='center')
sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)].head(5)
Sex | Sport | F | M | Sex_ratio |
---|---|---|---|---|
23 | Figure Skating | 1116 | 1066 | 1.046904 |
59 | Trampolining | 76 | 76 | 1.000000 |
60 | Triathlon | 263 | 266 | 0.988722 |
45 | Rugby Sevens | 148 | 151 | 0.980132 |
57 | Taekwondo | 299 | 307 | 0.973941 |
sport_sex_count[(sport_sex_count['F']!=0)&(sport_sex_count['M']!=0)].tail(5)
Sex | Sport | F | M | Sex_ratio |
---|---|---|---|---|
12 | Bobsleigh | 143 | 2189 | 0.065327 |
65 | Wrestling | 304 | 6539 | 0.046490 |
2 | Alpinism | 1 | 24 | 0.041667 |
50 | Ski Jumping | 30 | 1246 | 0.024077 |
13 | Boxing | 72 | 5966 | 0.012068 |
不同运动项目的性别比差异非常大。
女运动员占比最高的五项运动是花样滑冰、蹦床、铁人三项、七人制橄榄球和跆拳道。其中花样滑冰的性别比超过了1。
男运动员占比最高的三项运动是登山、跳台滑雪、拳击、摔跤和雪车。
运动员的参赛年龄
#夏季奥运会运动员的参赛年龄汇总
athlete_summer[['Age']].describe()
Age | |
---|---|
count | 152317.000000 |
mean | 25.907331 |
std | 6.277178 |
min | 10.000000 |
25% | 22.000000 |
50% | 25.000000 |
75% | 29.000000 |
max | 97.000000 |
#冬季奥运会运动员的参赛年龄汇总
athlete_winter[['Age']].describe()
Age | |
---|---|
count | 28368.000000 |
mean | 25.100783 |
std | 4.956407 |
min | 11.000000 |
25% | 22.000000 |
50% | 25.000000 |
75% | 28.000000 |
max | 58.000000 |
athlete_summer_m=athlete_summer[athlete_summer['Sex']=='M']
athlete_summer_f=athlete_summer[athlete_summer['Sex']=='F']
athlete_winter_m=athlete_winter[athlete_winter['Sex']=='M']
athlete_winter_f=athlete_winter[athlete_winter['Sex']=='F']
#绘制身高变化趋势,同时观察每届的身高范围
fig=plt.figure(figsize=(12,6),dpi=100)
ax=fig.add_subplot(111)
ax=sns.lineplot(x="Year", y="Age", data=athlete_summer_m,marker='.',label='summer male',color='#ed5a65')
ax=sns.lineplot(x="Year", y="Age", data=athlete_summer_f,marker='^',label='summer female',color='#ed5a65')
ax=sns.lineplot(x="Year", y="Age", data=athlete_winter_m,marker='.',label='winter male',color='#8abcd1')
ax=sns.lineplot(x="Year", y="Age", data=athlete_winter_f,marker='^',label='winter female',color='#8abcd1')
ax.set_xticks(np.linspace(1896,2016,num=31))
ax.set_title('历届奥运会运动员平均年龄变迁',fontsize=18)
ax.legend(loc=0)
早期奥运会的运动员年龄差异较大,后期逐渐趋于稳定,平均在25岁左右。
按夏季和冬季奥运会分,夏季奥运会运动员的平均年龄在大多数时期是大于冬季奥运会的,但到了近几届,这种差异已经非常小。
按性别分,除了少数几届运动会,男运动员的平均年龄均大于女运动员。
athlete_summer_sport_age=athlete_summer.loc[:,['Sport','Age']].groupby('Sport').mean().sort_values(by='Age',ascending=False).reset_index()
fig=plt.figure(figsize=(10,15),dpi=100)
ax=fig.add_subplot(111)
y=athlete_summer_sport_age['Age']
x=athlete_summer_sport_age.index
tick_label=athlete_summer_sport_age['Sport']
ax.barh(x,y,color='#ed5a65')
ax.set_yticks([i for i in range(len(athlete_summer_sport_age))])#设置y轴刻度,注意起始是1还是0
ax.set_yticklabels(tick_label)
ax.set_ylim(-1,52)
ax.get_xaxis().set_visible(False)#设置x轴不可见
ax.set_title("夏季奥运会各类运动平均参赛年龄",fontsize=18)
for x,y in enumerate(y):
ax.text(y+0.5,x,'{:.1f}'.format(y),va='center')
夏季奥运会不同比赛项目的平均年龄相差很大。Roque(短柄槌球)的平均年龄最高,达53.3岁。第二高的Art Competitions(艺术竞赛)为44.7岁。但这些项目仅出现在早期奥运会中,目前已不是夏季奥运会比赛项目。
平均年龄最小的项目是艺术体操,仅有18.7岁。
athlete_winter_sport_age=athlete_winter.loc[:,['Sport','Age']].groupby('Sport').mean().sort_values(by='Age',ascending=False).reset_index()
fig=plt.figure(figsize=(10,8),dpi=100)
ax=fig.add_subplot(111)
y=athlete_winter_sport_age['Age']
x=athlete_winter_sport_age.index
tick_label=athlete_winter_sport_age['Sport']
ax.barh(x,y,color='#c3d7df')
ax.set_yticks([i for i in range(len(athlete_winter_sport_age))])#设置y轴刻度,注意起始是1还是0
ax.set_yticklabels(tick_label)
ax.set_ylim(-1,17)
ax.get_xaxis().set_visible(False)#设置x轴不可见
ax.set_title("冬季奥运会各类运动平均参赛年龄",fontsize=18)
for x,y in enumerate(y):
ax.text(y+0.2,x,'{:.1f}'.format(y),va='center')
冬季奥运会不同运动项目的平均年龄差异不如夏季奥运会大。平均年龄最大的Alpinism(登山)也已不是冬季奥运会项目。
总的来看,不管是夏季奥运会还是冬季奥运会,平均年龄较大的项目都是对身体条件和状态要求相对较小,而对经验要求相对较高的项目,例如冰壶、马术、射击等。
而对身体要求较高的运动的平均年龄都较小,例如体操、游泳、花样滑冰、短道速滑等。
运动员的身体条件
#整理出运动员资料
athlete_phy=athlete.loc[:,['Height','Weight','Sex','Year','Season','Sport']]
#添加BMI列
athlete_phy['BMI']=athlete_phy['Weight']/(athlete_phy['Height']/100)**2
athlete_phy.head()
Height | Weight | Sex | Year | Season | Sport | BMI | |
---|---|---|---|---|---|---|---|
0 | 180.0 | 80.0 | M | 1992 | Summer | Basketball | 24.691358 |
1 | 170.0 | 60.0 | M | 2012 | Summer | Judo | 20.761246 |
2 | NaN | NaN | M | 1920 | Summer | Football | NaN |
3 | NaN | NaN | M | 1900 | Summer | Tug-Of-War | NaN |
4 | 185.0 | 82.0 | F | 1988 | Winter | Speed Skating | 23.959094 |
#夏季、冬季奥运会运动员资料分开
summer_phy=athlete_phy[athlete_phy['Season']=='Summer'].groupby(['Year','Sex']).mean().unstack()
winter_phy=athlete_phy[athlete_phy['Season']=='Winter'].groupby(['Year','Sex']).mean().unstack()
#夏季、冬季奥运会的男、女运动员资料分开
male_summer_phy=athlete_phy[(athlete_phy['Season']=='Summer')&(athlete_phy['Sex']=='M')]
female_summer_phy=athlete_phy[(athlete_phy['Season']=='Summer')&(athlete_phy['Sex']=='F')]
male_winter_phy=athlete_phy[(athlete_phy['Season']=='Winter')&(athlete_phy['Sex']=='M')]
female_winter_phy=athlete_phy[(athlete_phy['Season']=='Winter')&(athlete_phy['Sex']=='F')]
运动员的身高
运动员在变高
fig=plt.figure(figsize=(8,6),dpi=100)
ax=fig.add_subplot(111)
h_m=athlete_phy[athlete_phy['Sex']=='M'].loc[:,'Height'].dropna()
h_f=athlete_phy[athlete_phy['Sex']=='F'].loc[:,'Height'].dropna()
ax=sns.distplot(h_m,color='#36ADA4',label='male')
ax=sns.distplot(h_f,color='#F77189',label='female')
ax.set_title('运动员身高分布',fontsize=18)
ax.legend()
男运动员的身高整体高于女运动员。
#绘制身高变化趋势,同时观察每届的身高范围
fig=plt.figure(figsize=(12,6),dpi=100)
ax=fig.add_subplot(111)
ax=sns.lineplot(x="Year", y="Height", data=male_summer_phy,marker='.',label='male_summer',color='#ed5a65')
ax=sns.lineplot(x="Year", y="Height", data=female_summer_phy,marker='^',label='female_summer',color='#ed5a65')
ax=sns.lineplot(x="Year", y="Height", data=male_winter_phy,marker='.',label='male_winter',color='#8abcd1')
ax=sns.lineplot(x="Year", y="Height", data=female_winter_phy,marker='^',label='female_winter',color='#8abcd1')
ax.set_xticks(np.linspace(1896,2016,num=31))
ax.set_title('历届奥运会运动员平均身高变迁',fontsize=18)
ax.legend(loc=3)
早期运动员身高差异较大,后期运动员的身高差异相对越来越小,并且平均身高越来越高。
男运动员的平均身高高于女运动员。夏季奥运会的平均身高高于冬季运动会。
哪个项目的运动员最高?
#不分性别的各类运动平均身高
athlete_height=athlete_phy.loc[:,['Sport','Height']].dropna().groupby('Sport').mean().sort_values(by='Height',ascending=False).reset_index()
#男女分开的各类运动平均身高
athlete_height_m=athlete_phy[athlete_phy['Sex']=='M'].loc[:,['Sport','Height']].dropna().groupby('Sport').mean().sort_values(by='Height',ascending=False).reset_index()
athlete_height_f=athlete_phy[athlete_phy['Sex']=='F'].loc[:,['Sport','Height']].dropna().groupby('Sport').mean().sort_values(by='Height',ascending=False).reset_index()
#男女身高合并
height_m_f=pd.merge(athlete_height_m,athlete_height_f,how='outer',on='Sport').fillna(0).rename(columns=(dict(Height_x='M',Height_y='F')))
fig,ax=plt.subplots(figsize=(11,14),dpi=100)
h_m=height_m_f['M']
h_f=height_m_f['F']
x=np.arange(len(height_m_f))
tick_label=height_m_f['Sport']
ax.barh(x,h_m,color='#36ADA4',alpha=0.6,label='male')
ax.barh(x,h_f,color='#F77189',alpha=0.6,label='female')
ax.set_yticks(np.linspace(0,len(height_m_f)-1,num=len(height_m_f)))
#ax.set_xlim(150,197)
ax.set_ylim(-1,len(height_m_f))
ax.set_yticklabels(tick_label)
ax.set_title('各项运动平均身高',fontsize=18)
ax.legend()
#男运动员平均身高最高项目与最矮项目之差
height_m_f['M'].max()-height_m_f[height_m_f['M']>0]['M'].min()
27.25261090790002
#女运动员平均身高最高项目与最矮项目之差
height_m_f['F'].max()-height_m_f[height_m_f['F']>0]['F'].min()
26.462577158540597
不同运动项目的平均身高相差很大,男女平均身高最高的项目均是篮球,平均身高最矮的项目均是体操。项目平均身高差均达到26cm以上。
运动员的体重
运动员原来越重
fig=plt.figure(figsize=(8,6),dpi=100)
ax=fig.add_subplot(111)
w_m=athlete_phy[athlete_phy['Sex']=='M'].loc[:,'Weight'].dropna()
w_f=athlete_phy[athlete_phy['Sex']=='F'].loc[:,'Weight'].dropna()
ax=sns.distplot(w_m,color='#36ADA4',label='male')
ax=sns.distplot(w_f,color='#F77189',label='female')
ax.set_title('运动员体重分布',fontsize=18)
ax.legend()
男运动员的体重整体大于女运动员。女运动员的体重分布相对更为集中。
#绘制体重变化趋势,同时观察每届的体重范围
fig=plt.figure(figsize=(12,6),dpi=100)
ax=fig.add_subplot(111)
ax=sns.lineplot(x="Year", y="Weight", data=male_summer_phy,marker='.',label='male_summer',color='#ed5a65')
ax=sns.lineplot(x="Year", y="Weight", data=female_summer_phy,marker='^',label='female_summer',color='#ed5a65')
ax=sns.lineplot(x="Year", y="Weight", data=male_winter_phy,marker='.',label='male_winter',color='#8abcd1')
ax=sns.lineplot(x="Year", y="Weight", data=female_winter_phy,marker='^',label='female_winter',color='#8abcd1')
ax.set_xticks(np.linspace(1896,2016,num=31))
ax.set_title('历届奥运会运动员平均体重变迁',fontsize=18)
ax.legend(loc=3)
运动员体重的差异也是从大到小。体重变化呈现出升高的趋势,男运动员较为明显。
近几届冬夏奥运会的男运动员身高十分接近,但夏季奥运会女运动员的平均身高高于冬季奥运会女运动员。
哪个项目的运动员最重
#不分性别的各类运动平均体重
athlete_weight=athlete_phy.loc[:,['Sport','Weight']].dropna().groupby('Sport').mean().sort_values(by='Weight',ascending=False).reset_index()
#男女分开的各类运动平均体重
athlete_weight_m=athlete_phy[athlete_phy['Sex']=='M'].loc[:,['Sport','Weight']].dropna().groupby('Sport').mean().sort_values(by='Weight',ascending=False).reset_index()
athlete_weight_f=athlete_phy[athlete_phy['Sex']=='F'].loc[:,['Sport','Weight']].dropna().groupby('Sport').mean().sort_values(by='Weight',ascending=False).reset_index()
#合并男女体重到一张表
weight_m_f=pd.merge(athlete_weight_m,athlete_weight_f,how='outer',on='Sport').fillna(0).rename(columns=(dict(Weight_x='M',Weight_y='F')))
fig,ax=plt.subplots(figsize=(11,14),dpi=100)
w_m=weight_m_f['M']
w_f=weight_m_f['F']
x=np.arange(len(weight_m_f))
tick_label=weight_m_f['Sport']
ax.barh(x,w_m,color='#36ADA4',alpha=0.6,label='male')
ax.barh(x,w_f,color='#F77189',alpha=0.6,label='female')
ax.set_yticks(np.linspace(0,len(weight_m_f)-1,num=len(weight_m_f)))
ax.set_ylim(-1,len(weight_m_f))
ax.set_yticklabels(tick_label)
ax.set_title('各项运动平均体重',fontsize=18)
ax.legend()
男运动员平均体重最高的项目是tug of war(拔河),但目前已经取消。现存奥运项目男女平均体重最高的项目均为篮球,平均体重最低的项目均为体操。
运动员的BMI
运动员的BMI变化不大
fig=plt.figure(figsize=(8,6),dpi=100)
ax=fig.add_subplot(111)
BMI_m=athlete_phy[athlete_phy['Sex']=='M'].loc[:,'BMI'].dropna()
BMI_f=athlete_phy[athlete_phy['Sex']=='F'].loc[:,'BMI'].dropna()
ax=sns.distplot(BMI_m,color='#36ADA4',label='male')
ax=sns.distplot(BMI_f,color='#F77189',label='female')
ax.set_title('运动员BMI分布',fontsize=18)
ax.legend()
与身高、体重相比,运动员BMI指数的分布更为集中。
#绘制BMI变化趋势,同时观察每届的BMI范围
fig=plt.figure(figsize=(12,6),dpi=100)
ax=fig.add_subplot(111)
BMI_s_m=athlete_phy[(athlete_phy['Season']=='Summer')&(athlete_phy['Sex']=='M')]
BMI_s_f=athlete_phy[(athlete_phy['Season']=='Summer')&(athlete_phy['Sex']=='F')]
ax=sns.lineplot(x="Year", y="BMI", data=BMI_s_m,marker='.',label='male_summer',color='#ed5a65')
ax=sns.lineplot(x="Year", y="BMI", data=BMI_s_f,marker='^',label='female_summer',color='#ed5a65')
BMI_w_m=athlete_phy[(athlete_phy['Season']=='Winter')&(athlete_phy['Sex']=='M')]
BMI_w_f=athlete_phy[(athlete_phy['Season']=='Winter')&(athlete_phy['Sex']=='F')]
ax=sns.lineplot(x="Year", y="BMI", data=BMI_w_m,marker='.',label='male_winter',color='#8abcd1')
ax=sns.lineplot(x="Year", y="BMI", data=BMI_w_f,marker='^',label='female_winter',color='#8abcd1')
ax.set_xticks(np.linspace(1896,2016,num=31))
ax.set_title('历届奥运会运动员平均BMI变迁',fontsize=18)
ax.legend(loc=3)
早期奥运会运动员的BMI指数差异较大,近几届奥运会,BMI指数的差异已经减小很多。
与身高体重不同的是,BMI指数并没有呈现一个十分明显的升高或者降低的趋势。并且,虽然身高、体重方面都是夏季奥运会的运动员更高,但是BMI指数方面却是冬季奥运会的运动员更高。
哪个项目运动员的BMI最大
#不分性别的各类运动平均BMI
athlete_BMI=athlete_phy.loc[:,['Sport','BMI']].dropna().groupby('Sport').mean().sort_values(by='BMI',ascending=False).reset_index()
#男女分开的各类运动平均BMI
athlete_BMI_m=athlete_phy[athlete_phy['Sex']=='M'].loc[:,['Sport','BMI']].dropna().groupby('Sport').mean().sort_values(by='BMI',ascending=False).reset_index()
athlete_BMI_f=athlete_phy[athlete_phy['Sex']=='F'].loc[:,['Sport','BMI']].dropna().groupby('Sport').mean().sort_values(by='BMI',ascending=False).reset_index()
#合并男女BMI
BMI_m_f=pd.merge(athlete_BMI_m,athlete_BMI_f,how='outer',on='Sport').fillna(0).rename(columns=(dict(BMI_x='M',BMI_y='F')))
BMI_m_f.head()
Sport | M | F | |
---|---|---|---|
0 | Weightlifting | 28.035109 | 25.963200 |
1 | Tug-Of-War | 27.467685 | 0.000000 |
2 | Rugby Sevens | 27.169629 | 23.683783 |
3 | Bobsleigh | 27.125504 | 24.291185 |
4 | Judo | 26.205346 | 23.979138 |
fig,ax=plt.subplots(figsize=(11,14),dpi=100)
BMI_m=BMI_m_f['M']
BMI_f=BMI_m_f['F']
x=np.arange(len(BMI_m_f))
tick_label=BMI_m_f['Sport']
ax.barh(x,BMI_m,color='#36ADA4',alpha=0.6,label='male')
ax.barh(x,BMI_f,color='#F77189',alpha=0.6,label='female')
ax.set_yticks(np.linspace(0,len(BMI_m_f)-1,num=len(BMI_m_f)))
ax.set_ylim(-1,len(BMI_m_f))
ax.set_yticklabels(tick_label)
ax.set_title('各项运动平均BMI',fontsize=18)
ax.legend()
男女运动员平均BMI指数最高的项目均为举重。平均BMI指数最低的项目,男运动员是铁人三项,女运动员是艺术体操。