一、数据展示
1.1、数据
1.2、列名
字段名 | 备注 |
---|---|
Name | 姓名 |
Nationality | 国籍 |
National_Position | 国家队位置 |
National_Kit | 国家队号码 |
Club | 所在俱乐部 |
Club_Position | 所在俱乐部位置 |
Club_Kit | 俱乐部号码 |
Club_Joining | 加入俱乐部时间 |
Contract_Expiry | 合同到期时间 |
Rating | 评分 |
Height | 身高 |
Weight | 体重 |
Preffered_Foot | 擅长左(右)脚 |
Birth_Date | 出生日期 |
Age | 年龄 |
Preffered_Position | 擅长位置 |
Work_Rate | 工作效率 |
Weak_foot | 非惯用脚使用频率 |
Skill_Moves | 技术等级 |
Ball_Control | 控球技术 |
Dribbling | 盘球(带球)能力 |
Marking | 盯人能力 |
Sliding_Tackle | 铲球 |
Standing_Tackle | 逼抢能力 |
Aggression | 攻击能力 |
Reactions | 反映 |
Attacking_Position | 攻击性跑位 |
Interceptions | 抢断 |
Vision | 视野 |
Composure | 镇静 |
Crossing | 下底传中 |
Short_Pass | 短传 |
Long_Pass | 长传 |
Acceleration | 加速度 |
Speed | 速度 |
Stamina | 体力 |
Strength | 强壮 |
Balance | 平衡 |
Agility | 敏捷度 |
Jumping | 跳跃 |
Heading | 投球 |
Shot_Power | 射门力量 |
Finishing | 射门 |
Long_Shots | 远射 |
Curve | 弧线 |
Freekick_Accuracy | 任意球精准度 |
Penalties | 点球 |
Volleys | 凌空能力 |
GK_Positioning | 门将位置感 |
GK_Diving | 扑救能力 |
GK_Kicking | 门将踢球能力 |
GK_Handling | 扑球脱手几率 |
GK_Reflexes | 门将反应度 |
二、加载数据
2.1、加载足球运动员数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('FullData.csv')
2.2、设置中文和负数不显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
三、数据清洗
3.1、缺值处理
3.1.1、查看缺失值
df.info()
从上述示例可以看到总共17588行,但National_Position(国家队位置) 是1075行,Club_Position (俱乐部位置)17587行。我们知道有的足球运动员是没有进入国家队的,所以National_Position缺值是正常情况。但Club_Position缺值需要处理。
3.1.2、显示缺失值的数据
df[df['所在俱乐部位置'].isna()]
或
df[df['所在俱乐部位置'].notna()]
3.1.3、获取未缺失值的数据
df2 = df[~df['所在俱乐部位置'].isna()]
或
df2 = df[df['所在俱乐部位置'].notna()]
或
df2 = df[df['所在俱乐部位置'].notnull()]
3.2、异常值处理
3.2.1、数据描述统计信息
describe方法得到数据的描述性统计信息,比如max min,mean,std进行异常值分析
df2.describe()
3.2.2、使用箱线图辅助查看异常值
df2.boxplot(column='评分')
plt.show()
3.3、重复值处理
3.3.1、查看重复数据
df.duplicated().any()
3.3.2、查看没有重复的数据
df[~df.duplicated()]
3.3.3、删除重复的数据
df.drop_duplicates()
3.3.4、指定列重复才删除
df.drop_duplicates(subset=['姓名'])
3.3.5、替换原有的df数据
df.drop_duplicates(inplace=True)
四、数据可视化
4.1、运动员的体重分布情况
从查看数据结果可以看到运动员身高Height、体重Weight的数据后都添加了相应的单位。要分析运动员身高和体重的分布,首先需要将身高Height和Weight数据的单位去掉。
4.1.1、身高与体重处理
4.1.1.1、通过字符串的替换
df3['身高']=df3['身高'].str.replace('cm','').astype('int')
df3['体重']=df3['体重'].str.replace('kg','').astype('int')
4.1.1.2、通过apply方式
def handle_cm(v:str)-> int:
return int(v.replace('cm',''))
def handle_kg(v:str)-> int:
return int(v.replace('kg',''))
4.1.1.3、使用匿名函数的方式 lambda
df3['身高'].apply(lambda x:int(x.replace('cm','')))
df3['体重'].apply(lambda x:int(x.replace('kg','')))
4.1.2、 查看身高体重_数据分布情况
df3[['身高','体重']].describe()
直方图
plt.hist(df3['身高'],bins=20)
plt.title('身高')
plt.show()
plt.hist(df3['体重'],bins=20)
plt.title('体重')
plt.show()
画密度图
df3['身高'].plot(kind='kde')
plt.title('身高')
plt.show()
df3['体重'].plot(kind='kde')
plt.title('体重')
plt.show()
4.2、使用左右脚分析
4.2.1、使用饼状图来显示
df3['擅长左(右)脚'].value_counts().plot(kind='pie',autopct='%.2f%%',fontsize=16)
plt.legend()
plt.show()
4.2.2、value_counts()方法
df3['擅长左(右)脚'].value_counts()
4.2.3、 使用条形图
df3['擅长左(右)脚'].value_counts().plot(kind='bar')
plt.show()
4.3、俱乐部评分分析
4.3.1、获取前10的 俱乐部,根据球员的评分
dfg1=df3.groupby('所在俱乐部')
dfg1['评分'].mean().sort_values(ascending=False).head()
4.3.2、对俱乐部人数大于25人的俱乐部,平均评分进行排序取前10
# 查看俱乐部的球员人数,球员的平均分
rs1 = dfg1['评分'].agg(['mean','count'])
# 查看俱乐部的球员人数,球员的平均分,过滤掉人数小于25
rs1['count']>=25
# 查看俱乐部的球员人数,球员的平均分,过滤掉人数小于25,排名前10的俱乐部
rs1[rs1['count']>=25].sort_values(by='mean',ascending=False).head(10)
# 根据排名进行绘制图表
rs1[rs1['count']>=25].sort_values(by='mean',ascending=False).head(10).plot(kind='bar')
4.4、运动员与出生日期是否相关
4.4.1、获取足球运动员出生日期
data=df3['出生日期'].str.split('/',expand=True)
观察出生年和足球运动员数量关系
data['年'].value_counts().plot() plt.xlabel('年') plt.show()
观察出生月和足球运动员数量关系
data['月'].value_counts().plot() plt.xlabel('月') plt.show()
观察出生日和足球运动员数量关系
data['日'].value_counts().plot() plt.xlabel('日') plt.show()
4.4.2、评分大于等于80与出生日期关系
df4=df3[df3['评分']>80]
data2=df4['出生日期'].str.split('/',expand=True)
data2.columns = ['月', '日', '年']
观察出生年和足球运动员数量关系
data2['年'].value_counts().plot() plt.xlabel('年') plt.show()
观察出生月和足球运动员数量关系
data2['月'].value_counts().plot() plt.xlabel('月') plt.show()
观察出生日和足球运动员数量关系
data2['日'].value_counts().plot() plt.xlabel('日') plt.show()
4.5、身高与体重相关性
df3.plot(kind='scatter',x='身高',y='体重')
plt.show()
df3['身高'].corr(df3['体重'])
corr查看相关性
np.float64(0.7582641987537077)
4.6、年龄与评分相关性
df3['age']=pd.cut(df3['年龄'],bins=4,labels=['青年','中年','壮年','老年'])
df3.groupby('age')['评分'].mean().plot()
plt.show()
4.7、分析数据之间的相关性
df3.select_dtypes(include=['number']).corr()
查看哪些数据与评分相关性较强,得出前五名
df3.select_dtypes(include=['number']).corr()['评分'].sort_values(ascending=False).head(5)