1.导入必要的包
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline
2. 读取文件
nba=pd.read_csv('NBA球员数据.csv',encoding='gb2312')
nba.head()
3.进行探索性数据分析
(1) 查看球员年薪分布情况
salary=nba['球员薪金']
plt.figure("hist",figsize=(15,7))
n, bins, patches = plt.hist(salary, bins=30)
plt.show()
(2) 进攻能力对年薪的影响
# 进攻能力分为三个等级
nba['进攻能力分组']=pd.cut(nba['进攻能力'],[0,10,20,45],labels=['弱','中','强'])
plt.figure(figsize=(15,7))
sns.boxplot(x='进攻能力分组',y='球员薪金',data=nba)
可以看出,进攻能力与年薪有着明显的正相关关系。
(3) 防守能力对年薪的影响
# 防守能力分为三个等级
nba['防守能力分组']=pd.cut(nba['防守能力'],[-1,3,8,13],labels=['弱','中','强'])
plt.figure(figsize=(15,7))
sns.boxplot(x='防守能力分组',y='球员薪金',data=nba)
可以看出,防守能力与年薪也有着明显的正相关关系。
(4) 上场时间对年薪的影响
# 球员上场时间分为三个等级
nba['场均时间分组']=pd.cut(nba['场均时间'],[2,15,25,40],labels=['短','中','长'])
plt.figure(figsize=(15,7))
sns.boxplot(x='场均时间分组',y='球员薪金',data=nba)
上场时间和年薪有着正相关关系。
(5) 是否入选全明星
plt.figure(figsize=(10,7))
sns.boxplot(x='是否入选过全明星',y='球员薪金',data=nba)
可以看到,入选全明星的球员的年薪明显高于未入选全明星的球员。
(6) 球员年龄对收入的影响
# 球员年龄分为五个等级
nba['球员年龄分组']=pd.cut(nba['年龄'],[18,23,27,31,35,40],labels=['19-23','23-27','27-31','31-35','35-40'])
plt.figure(figsize=(15,7))
sns.boxplot(x='球员年龄分组',y='球员薪金',data=nba)
可以看到27-31的球员的平均年薪是最高的,19-23岁初出茅庐的球员的平均年薪最低。
(7) 球员球龄对收入的影响
(8) 球员场上位置对收入的影响
plt.figure(figsize=(15,7))
sns.boxplot(x='位置',y='球员薪金',data=nba,order=['控球后卫','大前锋','得分后卫','小前锋','中锋'])
下图分别为控球后卫,大前锋,得分后卫,小前锋,中锋收入箱形图:
(9) 球队胜率对球员年薪的影响
plt.figure(figsize=(15,7))
sns.boxplot(x='球队胜率',y='球员薪金',data=nba,order=['高','中','低'])