背景:

国际足联世界杯(FIFA World Cup)简称“世界杯”,是世界上最高荣誉、最高规格、最高竞技水平、最高知名度的足球比赛,与奥运会并称为全球体育两大最顶级赛事,影响力和转播覆盖率超过奥运会的全球最大体育盛事。

目的:

本文旨在分析1930-2014年历届世界杯数据,并进行可视化展示,目的是了解以下内容:

  • 历届世界杯的观众总数以及每届世界杯平均每场比赛的观众数量
  • 历届世界杯的进球数量比较
  • 历届世界杯中观众最多的比赛
  • 历届世界杯中接纳过最多观众的比赛场地
  • 举办过最多次世界杯的城市
  • 各国的冠亚季军获得情况,通过模拟积分制判断哪个国家在历届世界杯中表现最好

工具:

通过使用Python及pandas,matplotlib,seaborn,numpy等库的使用,对数据进行清洗、整理、转换、分析

结果: 得到想要结果的同时,对于分析过程中发现的异常数据,通过查阅资料以及独立思考,有了自己的理解(见文末总结)

一.数据载入和预览

有两份数据,一份是历届世界杯的比赛数据,一份是决赛数据

import os
import pandas as pd

# 声明数据集路径
data_path = ./data
matches_datafile = os.path.join(data_path, WorldCupMatches.csv)  # 比赛数据集
cups_datafile = os.path.join(data_path, WorldCups.csv)  # 决赛数据集 

# 读取数据
matches_df = pd.read_csv(matches_datafile)
cups_df = pd.read_csv(cups_datafile)

#查看比赛数据信息

历届世界杯数据分析

可以看到,比赛数据中包含了4572行,20列数据。每列数据的数据类型也可以看到。但是通过分析每列的数量以及查看数据源,发现其实有效数据只有852行,其余的都是空数据

查看比赛数据的前5行

#比赛数据
matches_df.head()

历届世界杯数据分析

查看决赛数据信息

cups_df.info()
cups_df.head()

历届世界杯数据分析

历届世界杯数据分析

二.数据分析

主要分析几个角度:

  • 历届世界杯的观众总数以及每届世界杯平均每场比赛的观众数量
  • 历届世界杯的进球数量比较
  • 历届世界杯中观众最多的比赛
  • 历届世界杯中接纳过最多观众的比赛场地
  • 举办过最多次世界杯的城市
  • 各国的冠亚季军获得情况,通过模拟积分制判断哪个国家在历届世界杯中表现最好

2.1 历届世界杯的观众

世界杯自1930年起,每4年举办一次,除了1942年和1946年,由于二战的原因而没有举办。首先分析历届世界杯的观众数量

#载入相关库
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style(whitegrid)

首先查看是否有重复记录。每场比赛的ID应该都是唯一的,可以根据比赛ID查看记录是否重复

matches_df[MatchID].value_counts().head()

历届世界杯数据分析

可以看到是存在一些重复记录的,需要先删除

# 去重
matches_df = matches_df.drop_duplicates([MatchID])

#之前说过,很多数据都是缺失的空行,需要把缺失的数据丢掉
matches_df = matches_df[matches_df["Year"].notnull()]
matches_df.info()

历届世界杯数据分析

可以看到,去掉了所有的缺失行以后,剩下836行,20列的有效数据。接下来开始分析历届世界杯的观众人数

sum_att_df = matches_df.groupby(Year)[Attendance].sum().reset_index()
sum_att_df[Year] = sum_att_df[Year].astype(int)
sum_att_df

历届世界杯数据分析

#历届世界杯人数可视化
plt.figure(figsize=(12, 6))
sns.barplot(sum_att_df[Year], sum_att_df[Attendance], linewidth=1,color=b)
plt.title(Attendence by year)
plt.show()

历届世界杯数据分析

#求出观众数最多和最少的五年
sum_att_df.sort_index(by=Attendance,ascending=False).head()
sum_att_df.sort_index(by=Attendance,ascending=False).tail()

历届世界杯数据分析

世界杯年份和观众数量

通过分析可以得到历届世界杯的观众数最多和最少的5年。最高是1994年的世界杯,观众数达到358.7万人;最少的是1934年,有36.3万人

知道了每届比赛的总人数,那么每届比赛中,平均每场比赛的观众数量是多少呢?

# 每届世界杯平均每场比赛观众人数
att_per_game_df = matches_df.groupby(Year)[Attendance].mean().reset_index()
att_per_game_df[Year] = att_per_game_df[Year].astype(int)
att_per_game_df

历届世界杯数据分析

# 每届世界杯每场比赛观众人数可视化
plt.figure(figsize=(12, 6))
ax = sns.pointplot(att_per_game_df[Year], att_per_game_df[Attendance])
plt.title(Average attendence by year)
plt.show()

历届世界杯数据分析

由上述分析可知,自1930年以来,世界杯的观众人数总体呈上升趋势。人数最多的时候在1994年世界杯,达到358.7万人;最少的是1934年,有36.3万人。

2.2 历届世界杯进球数

cups_df[[Year,GoalsScored]]

历届世界杯数据分析

#可视化
import numpy as np
plt.figure(figsize=(14, 6))
ax = plt.scatter(Year, GoalsScored, data=cups_df, 
                 c=cups_df[GoalsScored], cmap=inferno, 
                 s=800, alpha=.7, linewidth=2, edgecolor=k)

plt.colorbar()
plt.xticks(cups_df[Year].unique())
plt.title(Total goals scored by year)
plt.xlabel(year)
plt.ylabel(total goals scored)
plt.show()

历届世界杯数据分析

历届世界杯的进球数总体呈现上升趋势,最多的时候是1998和2014年,达到了171个进球,最少的时候是世界杯开始举办时的两年,只有70个进球

2.3 历届世界杯观众最多的比赛

分析历届世界杯中观众最多的10场比赛

# 最多观众的10场比赛
top_att_records = matches_df.sort_values(by=Attendance, ascending=False)[:10]
top_att_records = top_att_records[[Year, Datetime,Stadium, City, Home Team Name, 
                                   Home Team Goals, Away Team Goals, Away Team Name, 
                                   Attendance, MatchID]]

top_att_records[Match Team] = top_att_records[Home Team Name] +  .VS.  \
+ top_att_records[Away Team Name]

top_att_records[[Datetime,Match Team,Stadium,Attendance]]

历届世界杯数据分析

#可视化
plt.figure(figsize=(10, 8))
ax = sns.barplot(y=top_att_records[Match Team], x=top_att_records[Attendance],  
                 linewidth=1, edgecolor=k)
plt.ylabel(Match Teams)
plt.xlabel(Attendance)
plt.title(Matches with highest number of attendace)
plt.grid(True)
# 添加文本
for i, j in enumerate( Stadium:  + top_att_records[Stadium] + , Date:  +\
 top_att_records[Datetime]):
    ax.text(.7, i, j, fontsize=12, color=white, weight=bold)
plt.show()

历届世界杯数据分析

通过分析可知,观众数量最多的三场比赛分别是:

1st:1950年7月16日,乌拉圭和巴西的比赛

2nd:1950年7月13日,巴西和西班牙的比赛

3rd:1950年7月1日,巴西和南斯拉夫的比赛

2.4 历届世界杯接待过观众最多的比赛场地

#观众最多的10个场地
stadium_city_df = matches_df.groupby([Stadium, City])[Attendance].sum().reset_index()
stadium_city_df = stadium_city_df.sort_values(by=Attendance, ascending=False)[:10]
stadium_city_df

历届世界杯数据分析

#可视化
plt.figure(figsize=(10, 8))
ax = sns.barplot(y=stadium_city_df[Stadium], x=stadium_city_df[Attendance],\
linewidth=1,edgecolor=k)

# 添加文本
for i,j in enumerate( City:  + stadium_city_df[City]):
    ax.text(.7, i, j, fontsize=12)
plt.title(Stadiums with highest sum attendance)
plt.xlabel(Attendance)
plt.show()

历届世界杯数据分析

2.5 历届世界杯中举办过最多比赛的城市

#可视化
mat_c = matches_df["City"].value_counts().reset_index()
plt.figure(figsize=(10,8))
ax = sns.barplot(y=mat_c["index"][:15],x = mat_c["City"][:15],
                 linewidth=1,edgecolor="k"*15)
plt.xlabel("number of matches")
plt.ylabel("City")
plt.title("Cities with maximum world cup matches",color=b)

for i,j in enumerate("Matches  :" + mat_c["City"][:15].astype(str)):
    ax.text(.7,i,j,fontsize = 13,color="w")
plt.show()

历届世界杯数据分析

2.6 获得冠军最多的国家

#获取每个国家获得冠军的年份
cups_df.replace(Germany FR, Germany, inplace=True)
cups_df[Year_str] = cups_df[Year].astype(str)

winner_years_df = cups_df.groupby(Winner)[Year_str].apply(, .join).reset_index()
winner_years_df.head()

历届世界杯数据分析

#获取给个国家获得冠军的次数
winner_years_count_df  = cups_df.groupby(Winner)[Year].count().reset_index()
winner_years_count_df.head()

历届世界杯数据分析

#组合上面的结果,并且按冠军次数排序
winner_years_merge_df = winner_years_df.merge(winner_years_count_df, left_on=Winner,\
 right_on=Winner, how=left)
winner_years_merge_df.sort_values(by=Year, ascending=False, inplace=True)
winner_years_merge_df.head()

历届世界杯数据分析

#可视化
# 可视化结果
plt.figure(figsize=(10, 8))
ax = sns.barplot(Year, Winner, data=winner_years_merge_df,
            palette=jet_r, alpha=.8, linewidth=2, edgecolor=k)
for i,j in enumerate(Years:  + winner_years_merge_df[Year_str]):
    ax.text(.1, i, j, weight=bold)

plt.title(Teams with the most world cup final victories)
plt.xlabel(count)
plt.show()

历届世界杯数据分析

根据分析,获得世界杯冠军最多的国家是巴西,总共获得了5次冠军,分别在1958年,1962年,1970年,1994年和2002年

2.7 历届世界杯冠亚季军情况

#冠军
winner_df=cups_df[Winner].value_counts().reset_index()
winner_df.columns=[Country,Count]
winner_df[Type]=WINNER
winner_df[Score]=3
winner_df

#亚军
runnerup_df=cups_df[Runners-Up].value_counts().reset_index()
runnerup_df.columns = [Country, Count]
runnerup_df[Type] = RUNNER-UP
runnerup_df[Score]=2

#季军
Third_df=cups_df[Third].value_counts().reset_index()
Third_df.columns = [Country, Count]
Third_df[Type] = Third
Third_df[Score]=1

#合并
conc_df=pd.concat([winner_df,runnerup_df,Third_df])
conc_df

历届世界杯数据分析

统计了历届世界杯获得冠亚季军的国家和次数,并且给冠军赋予3个分值,亚军2个分值,季军1个分值,以此来计算世界杯历史上获得荣誉最多的国家

# 可视化结果
plt.figure(figsize=(8, 10))
sns.barplot(Count, Country, data=conc_df,hue=Type,linewidth=1, edgecolor=k)
plt.legend(loc=center right, prop={size: 12})
plt.title(Final results by nation)
plt.xlabel(Count)
plt.show()

历届世界杯数据分析

#假设冠军积3分,亚军积2分,季军积1分,分析历届世界杯以来,各国家的积分情况
conc_df[Sum_score]=conc_df[Count]*conc_df[Score]
result_df=conc_df.groupby(Country)[Sum_score].sum().reset_index()
result_df.sort_values(by=Sum_score,ascending=False,inplace=True)
result_df

历届世界杯数据分析

#可视化
sns.barplot(x=Sum_score,y=Country,data=result_df,palette="Blues_d")
plt.title(Result)
plt.xlabel(Score)
plt.show()

历届世界杯数据分析

#统计积分最多的3个国家的获奖情况
C_list=list(result_df.head(3).Country.values)
C_list
my_df=conc_df[conc_df[Country].isin(C_list)].sort_values(by=Country)
C_dict={a:b for a,b in zip(result_df[Country],result_df[Sum_score])}
C_dict
my_df[Sum_score]=my_df[Country].map(C_dict)
my_df.sort_values(by=[Sum_score,Score],ascending=False)

历届世界杯数据分析

根据上述分析,历届世界杯(1930~2014)中,如果获得一次冠军积3分,一次亚军积2分,一次季军积1分,那么积分最高的3个国家分别为:

1st.德国:24分,4次冠军,4次亚军,4次季军

2nd.巴西:21分,5次冠军,2次亚军,2次季军

3rd.意大利:17分,4次冠军,2次亚军,1次季军

三.总结

世界杯的话题经久不衰,虽然不是球迷,但是通过对历届世界杯数据的分析,可以对世界杯有个大体的了解。

而且在数据分析过程中,会发现很多很有意思的事情,比如对历届世界杯观众总数和观看人数最多的比赛分析可以发现一个很有意思的现象:观众总人数最多是在1994年,达到358.7万;1950年的观众总数只有104.5万人,但是观看人数最多的4场比赛都是在1950年,而且都是巴西队的比赛。

那么1950年世界杯发生了什么,使得观看总人数不到人数最多一届世界杯的1/3,而巴西队的四场比赛却成为世界杯历史上观看人数最多的比赛?

翻阅资料了解到,94年世界杯在美国举办,而且仅此一次。众所周知美国经济实力强大,能吸引大批球迷到美国观看比赛;而美国队过早被淘汰,本国球迷关注某一场比赛的激情不高。反观1950年,世界杯在巴西举办,巴西队一路杀入决赛,受到本国球迷的热烈追捧,球迷对比赛关注度高,可以看到最受欢迎的4场比赛都是巴西队的比赛。另外,由于二战原因,1942年和1946年的世界杯都没有举办,1950年是12年来的唯一 一次世界杯。刚经过残酷的战争,在如此盛大的运动盛典上,巴西队本土作战,势不可挡,一路挺进决赛,这对于巴西人民来说,无疑是莫大的鼓舞,战争带来的苦楚全部转化为民族自豪感和呐喊,或许正是由于这特殊的历史背景,才使得当年巴西和乌拉圭的决赛成为世界杯有史以来观看人数最多的比赛吧。