163Python数据分析师课程考核项目04
多场景下的图表可视化表达
excel数据概览
运动员信息
运动员CP热度
题目1
2016年奥运运动员数据,数据格式为xlsx,分3个sheet 1、分男女分别分析运动员的身高分布,并制作图表,数据为“奥运运动员数据.xlsx,sheet → 运动员信息” 要求: ① 制作分布密度图② 计算出男女平均身高,并绘制辅助线表示 提示: ① 可视化制图方法 → sns.distplot()② 辅助线制图方法 → plt.axvline()③ 分男女分别筛选数据并制作图表④ 不需要创建函数
题目1思路
1. 创建工作路径,查看读取数据。 2. 提取身高信息,提取性别和身高,去除空值。 3. 根据男女分类,reset_index()重新设置标签 4. 计算平均值 5. 绘制分布密度图,创建绘图区域,设置图表大小,绘制平均身高辅助线。
题目1 代码
import os
os
.chdir
('C:\\Users\\WQQ\\Desktop\\163data\\项目题目和项目答案\\考核项目04_多场景下的图表可视化表达\\')
df
=pd
.read_excel
('奥运运动员数据.xlsx',sheetname
='运动员信息')
df_len
=len(df
)
df_col
=df
.columns
.tolist
()
data_height
=pd
.DataFrame
(df
['gender'],df
['height'])
data_height
.dropna
(inplace
=True)
data_male
=data_height
.groupby
(['gender']).get_group
('男').reset_index
()
data_famale
=data_height
.groupby
(['gender']).get_group
('女').reset_index
()
male_mean
=data_male
['height'].mean
()
famale_mean
=data_famale
['height'].mean
()
print('男性运动员平均身高:%i,\n女性运动员平均身高:%i'
%(male_mean
,famale_mean
))
5. 绘制分布密度图,创建绘图区域,设置图表大小,绘制平均身高辅助线。
plt
.figure
(figsize
=(6,4))
sns
.distplot
(data_male
['height'],hist
=False,kde
=True,rug
=True,color
='blue',axlabel
='运动员身高',label
='男性运动员身高分布')
sns
.distplot
(data_famale
['height'],hist
=False,kde
=True,rug
=True,color
='red',axlabel
='运动员身高',abel
='女性运动员身高分布')
plt
.grid
()
plt
.axvline
(male_mean
,color
='blue',linestyle
=':')
plt
.text
(male_mean
+2,0.005,'男性运动员身高平均值:%i' %male_mean
,color
='blue')
plt
.axvline
(famale_mean
,color
='red',linestyle
='--')
plt
.text
(famale_mean
,0.003,'女性运动员身高平均值:%i' %famale_mean
,color
='red')
题目1成果
题目2
2、综合指标判断运动员的身材,并找到TOP8的运动员,并制作图表,数据为“奥运运动员数据.xlsx,sheet → 运动员信息” 要求:① 针对不同指标,绘制面积堆叠图② TOP8的运动员,绘制雷达图表示 提示:① 四个指标评判运动员身材,并加权平均 a. BMI 指数(BMI =体重kg ÷ 身高m2,详细信息可百度查询)→ 越接近22分数越高 b. 腿长/身高 指数 → 数据筛选,只选取小于0.7的数据,越大分数越高 c. 臂展/身高 指数 → 数据筛选,只选取大于0.7的数据,比值越接近1分数越高 d. 年龄 指数 → 年龄越小分数越高 对上述abcd指标分别标准化得到n1,n2,n3,n4(划分到0-1的分值), 最后评分: finalscore = (n1 + n2 + n3 + n4)/4 ② 制作堆叠面积图,data.plot.area() ③ 雷达图需要构建子图 + for循环遍历得到**
题目2思路
1.创建工作路径,查看读取数据; 2.选出需要进行处理的数据,计算四个指标:BMI,筛选出腿长/身高<0.7,筛选出臂展/身高>0.7,年龄指数; 3. 参数评估:BMI 越接近22分数越高;腿长/身高 越大分数越高;臂展/身高比值越接近1分数越高;年龄指数年龄越小分数越高; 4. 对上述指标分别标准化得到n1,n2,n3,n4,划分到0-1的分值; 5. 最后评分: finalscore = (n1 + n2 + n3 + n4)/4; 6. 排序设定index; 7. 绘制面积图。 8. 筛选出top08的运动员 9. 分别绘制每个运动员的评分雷达图
题目2 代码
import os
os
.chdir
('C:\\Users\\WQQ\\Desktop\\163data\\项目题目和项目答案\\考核项目04_多场景下的图表可视化表达\\')
df
=pd
.read_excel
('奥运运动员数据.xlsx',sheetname
='运动员信息')
df_len
=len(df
)
df_col
=df
.columns
.tolist
()
data
=df
[['name','birthday','height','arm','leg','weight','age']]
data
.dropna
(inplace
=True)
data02
=data
.reset_index
()
data02
['BMI']=data02
['weight']/(data02
['height']/100)**2
data02
['leg/height']=data02
['leg']/data02
['height']
data02
['arm/height']=data02
['arm']/data02
['height']
data02
['age']
data01
=data02
[['name','BMI','leg/height','arm/height','age']]
data01
=data01
[data01
['leg/height']<0.7]
data01
=data01
[data01
['arm/height']>0.7]
data01
['reBMI']=np
.abs(data01
['BMI']-22)
data01
['releg/height']=data01
['leg/height']
data01
['rearm/height']=np
.abs(data01
['arm/height']-1)
data01
['reage']=data01
['age']
data01
['BMI_sta']=(data01
['reBMI'].max()-data01
['reBMI'])/(data01
['reBMI'].max()-data01
['reBMI'].min())
data01
['leg/h_sta']=(data01
['releg/height']-data01
['releg/height'].min())/(data01
['releg/height'].max()-data01
['releg/height'].min())
data01
['arm/h_sta']=(data01
['rearm/height'].max()-data01
['rearm/height'])/(data01
['rearm/height'].max()-data01
['rearm/height'].min())
data01
['age_sta']=(data01
['reage'].max()-data01
['reage'])/(data01
['reage'].max()-data01
['reage'].min())
data01
['final']=(data01
['BMI_sta']+data01
['leg/h_sta']+data01
['arm/h_sta']+data01
['age_sta'])/4
plt
.figure
(figsize
=(10,6))
data01
.sort_values
(by
='final',inplace
=True,ascending
=False)
data01
.reset_index
(inplace
=True)
data01
[['BMI_sta','leg/h_sta','arm/h_sta','age_sta']].plot
.area
(colormap
='autumn',alpha
=0.3,figsize
=(10,6))
plt
.ylim
([0,4])
plt
.grid
()
datatop08
=data01
[:8]
datatop08
=datatop08
[['name','BMI_sta','leg/h_sta','arm/h_sta','age_sta','final']]
fig
=plt
.figure
(figsize
=(16,8))
plt
.subplots_adjust
(wspace
=0.35,hspace
=0.5)
n
= 0
for i
in datatop08
['name'].tolist
():
n
+= 1
axi
= plt
.subplot
(2,4,n
, projection
= 'polar')
datai
= datatop08
[['BMI_sta','leg/h_sta','arm/h_sta','age_sta']][datatop08
['name']==i
].T
scorei
= datatop08
['final'][datatop08
['name']==i
]
angles
= np
.linspace
(0, 2*np
.pi
, 4, endpoint
=False)
plt
.polar
(angles
, datai
, 'o-', linewidth
=1)
axi
.fill
(angles
,datai
,alpha
=0.3)
axi
.set_thetagrids
(np
.arange
(0.0, 360.0, 90),['BMI','腿长/身高','臂长/身高','年龄'])
axi
.set_rgrids
(np
.arange
(0.2,1.5,0.2),'--')
plt
.title
('Top%i %s: %.3f\n' %(n
,i
,scorei
))
题目成果
运动员身体指标面积图
top08运动员的身体指标雷达图
题目3
根据运动员CP数据,分析出CP综合热度,通过python处理数据并导出,在Gephi中绘制图表,数据为“奥运运动员数据.xlsx,sheet → 运动员CP热度” 要求:① 用python计算出综合热度指标② 用Gephi绘制关系可视化图表 提示:① 三个指标评判运动员CP综合热度,并加权平均 a. cp微博数量 → 数量越多分数越高 b. cp微博话题阅读量 → 阅读量越多分数越高 c. B站cp视频播放量 → 播放量越大分数越高 对上述abcd指标分别标准化得到n1,n2,n3,n4(划分到0-1的分值),最后评分: finalscore = n10.5 + n20.3 + n30.2 ② Gephi中布局模式选择“ForceAtlas2” ③ Gephi中通过模块化计算,给关联结果做分组,并且以此分组设定点颜色***
题目3思路
1. 加载读取数据 2. 数据清洗: 3. 计算综合指标: a. cp微博数量 → 数量越多分数越高 b. cp微博话题阅读量 → 阅读量越多分数越高 c. B站cp视频播放量 → 播放量越大分数越高 4. 最后评分: finalscore = n10.5 + n20.3 + n30.2 5. 数据排序设定标签 6. 导出excel文件***
题目3 代码
import os
os
.chdir
('C:\\Users\\WQQ\\Desktop\\163data\\项目题目和项目答案\\考核项目04_多场景下的图表可视化表达\\')
df
=pd
.read_excel
('奥运运动员数据.xlsx',sheetname
='运动员CP热度')
df_len
=len(df
)
df_col
=df
.columns
.tolist
()
df
.replace
([np
.nan
,'无数据'],0,inplace
=True)
df
['微博排行']=(df
['cp微博数量']-df
['cp微博数量'].min())/(df
['cp微博数量'].max()-df
['cp微博数量'].min())
df
['阅读排行']=(df
['cp微博话题阅读量']-df
['cp微博话题阅读量'].min())/(df
['cp微博话题阅读量'].max()-df
['cp微博话题阅读量'].min())
df
['B站排行']=(df
['B站cp视频播放量']-df
['B站cp视频播放量'].min())/(df
['B站cp视频播放量'].max()-df
['B站cp视频播放量'].min())
df
['final']=df
['微博排行']*0.5+df
['阅读排行']*0.3+df
['B站排行']*0.2
df
.sort_values
('final',inplace
=True,ascending
=False)
df
.reset_index
(inplace
=True)
result
=df
[['p1','p2','final']]
writer
=pd
.ExcelWriter
('CP_sort.xlsx')
result
.to_excel
(writer
,'sheet1')
writer
.save
题目成果
根据‘箭头’大小能看粗,福原爱-张怡宁,林丹-李宗伟,孙杨-朴泰恒,宁泽涛-孙燕姿啊,傅园慧-孙杨,田亮-叶一西,以上这几对CP最热门。