import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
df = pd.read_csv('results.csv')
df.head()
1、 获取所有世界杯比赛的数据(不含预选赛)
df_FIFA_all = df[df['tournament'].str.contains('FIFA', regex=True)]
df_FIFA = df_FIFA_all[df_FIFA_all['tournament']=='FIFA World Cup']
df_FIFA.head()
结果如下:
▼
数据做一个初步整理
df_FIFA.loc[:,'date'] = pd.to_datetime(df_FIFA.loc[:,'date'])
df_FIFA['year'] = df_FIFA['date'].dt.year
df_FIFA['diff_score'] = df_FIFA['home_score']-df_FIFA['away_score']
df_FIFA['win_team'] = ''
df_FIFA['diff_score'] = pd.to_numeric(df_FIFA['diff_score'])
创建一个新的列数据,包含获胜队伍的信息
# The first method to get the winners
df_FIFA.loc[df_FIFA['diff_score']> 0, 'win_team'] = df_FIFA.loc[df_FIFA['diff_score']> 0, 'home_team']
df_FIFA.loc[df_FIFA['diff_score']< 0, 'win_team'] = df_FIFA.loc[df_FIFA['diff_score']< 0, 'away_team']
df_FIFA.loc[df_FIFA['diff_score']== 0, 'win_team'] = 'Draw'
df_FIFA.head()
# The second method to get the winners
def find_win_team(df):
winners = []
for i, row in df.iterrows():
if row['home_score'] > row['away_score']:
winners.append(row['home_team'])
elif row['home_score'] < row['away_score']:
winners.append(row['away_team'])
else:
winners.append('Draw')
return winners
df_FIFA['winner'] = find_win_team(df_FIFA)
df_FIFA.head()
结果如下:
▼
2、 获取世界杯所有比赛的前20强数据情况
s = df_FIFA.groupby('win_team')['win_team'].count()
s.sort_values(ascending=False, inplace=True)
s.drop(labels=['Draw'], inplace=True)
用pandas可视化如下:
柱状图
s.head(20).plot(kind='bar', figsize=(10,6), title='Top 20 Winners of World Cup')
▼
水平柱状图
s.sort_values(ascending=True,inplace=True)
s.tail(20).plot(kind='barh', figsize=(10,6), title='Top 20 Winners of World Cup')
▼
饼图
s_percentage = s/s.sum()
s_percentage
s_percentage.tail(20).plot(kind='pie', figsize=(10,10), autopct='%.1f%%',
startangle=173, title='Top 20 Winners of World Cup', label='')
▼
分析结论1:
从赢球场数来看,巴西、德国、意大利、阿根廷四支球队实力最强。
通过上面的分析,我们还可以来查看部分国家的获胜情况
s.get('China', default = 'NA')
s.get('Japan', default = 'NA')
s.get('Korea DPR', default = 'NA')
s.get('Korea Republic', default = 'NA')
s.get('Egypt', default = 'NA')
运行结果分别是 ‘NA’,4,1,5,‘NA’。
从结果来看,中国队,在世界杯比赛上(不含预选赛)还没有赢过。当然,本次世界杯的黑马-埃及队,之前两度进入世界杯上,但也没有赢过~~
上面分析的是赢球场数的情况,下面我们来看下进球总数情况。
df_score_home = df_FIFA[['home_team', 'home_score']]
column_update = ['team', 'score']
df_score_home.columns = column_update
df_score_away = df_FIFA[['away_team', 'away_score']]
df_score_away.columns = column_update
df_score = pd.concat([df_score_home,df_score_away], ignore_index=True)
s_score = df_score.groupby('team')['score'].sum()
s_score.sort_values(ascending=False, inplace=True)
s_score.sort_values(ascending=True, inplace=True)
s_score.tail(20).plot(kind='barh', figsize=(10,6), title='Top 20 in Total Scores of World Cup')
▼
team_list = ['Russia', 'Germany', 'Brazil', 'Portugal', 'Argentina', 'Belgium', 'Poland', 'France',
'Spain', 'Peru', 'Switzerland', 'England', 'Colombia', 'Mexico', 'Uruguay', 'Croatia',
'Denmark', 'Iceland', 'Costa Rica', 'Sweden', 'Tunisia', 'Egypt', 'Senegal', 'Iran',
'Serbia', 'Nigeria', 'Australia', 'Japan', 'Morocco', 'Panama', 'Korea Republic', 'Saudi Arabia']
for item in team_list:
if item not in s_score.index:
print(item)
out:
Iceland
Panama
通过上述分析可知,冰岛队和巴拿马队是首次打入世界杯的。
由于冰岛队和巴拿马队是首次进入世界杯,所以这里的32强数据,事实上是没有这两支队伍的历史数据的。
df_top32 = df_FIFA[(df_FIFA['home_team'].isin(team_list))&(df_FIFA['away_team'].isin(team_list))]
赢球场数情况
s_32 = df_top32.groupby('win_team')['win_team'].count()
s_32.sort_values(ascending=False, inplace=True)
s_32.drop(labels=['Draw'], inplace=True)
s_32.sort_values(ascending=True,inplace=True)
s_32.plot(kind='barh', figsize=(8,12), title='Top 32 of World Cup since year 1872')
▼
进球数据情况
df_score_home_32 = df_top32[['home_team', 'home_score']]
column_update = ['team', 'score']
df_score_home_32.columns = column_update
df_score_away_32 = df_top32[['away_team', 'away_score']]
df_score_away_32.columns = column_update
df_score_32 = pd.concat([df_score_home_32,df_score_away_32], ignore_index=True)
s_score_32 = df_score_32.groupby('team')['score'].sum()
s_score_32.sort_values(ascending=False, inplace=True)
s_score_32.sort_values(ascending=True, inplace=True)
s_score_32.plot(kind='barh', figsize=(8,12), title='Top 32 in Total Scores of World Cup since year 1872')
▼
分析结论3:
自1872年以来,32强之间的世界杯比赛,从赢球场数和进球数量来看,德国、巴西、阿根廷三支球队实力最强。
自1872年到现在,已经有100多年,时间跨度较大,有些国家已发生重大变化,后续分别分析自1978年(近10届)以及2002年(近4届)以来的比赛情况。
程序代码是类似的,这里只显示可视化的结果。
▼
进球数据情况
▼
分析结论4:
自1978年以来,32强之间的世界杯比赛,从赢球场数来看,阿根廷、德国、巴西三支球队实力最强。从进球数量来看,前3强也是这三支球队,但德国队的数据优势更明显。
赢球场数情况
▼
进球数据情况
▼
文章来自公众号Python数据之道