Jupyter 学习记录

#期中考试准备#

考前做的一道比较综合的题目~

读取StudentPerformance.csv文件将数据保存为data

import pandas as pd
import numpy as np
from pandas import Series,DataFrame
data = pd.read_csv('StudentsPerformance.csv')
data.iloc[0:5,:]

判断data是否存在缺失数据, 若有,对该字段进行向上填充,并替换原来的数据

data.isnull()
data.fillna(method='ffill',inplace=True)

新增一列mean_scores,计算每个学生的三门课程平均得分

mean_scores = data[['math score','reading score','writing score']].mean(axis = 1)
mean_scores
data['mean_scores'] = mean_scores
data.iloc[0:5,:]

筛选mean_scores 大于80的记录,保存到csv文件,命名为data80.csv。列索引保留,行索引不保留。

data[mean_scores.values > 80].to_csv('data80.csv',index = 0)

 按mean_scores对data进行降序排序,并替换原来的数据,输出排名前十的学生信息

data.sort_values(by='mean_scores',ascending=False,inplace=True)
data[0:10]

按mean_scores对data进行降序排序,并替换原来的数据,输出排名前十的学生信息

data.groupby('race/ethnicity')['math score'].mean()
data.groupby('race/ethnicity')['reading score'].max()

计data中test preparation course两种类别none、completed的学生数量

data.groupby('test preparation course').count()

按性别绘制学生平均成绩(mean_scores)的箱须图(boxplot)

#按性别绘制学生平均成绩(mean_scores)的箱须图(boxplot)
import matplotlib.pyplot as plt
data1 = data.groupby('gender')['mean_scores'].apply(list).values
plt.boxplot(data1,labels=('female','male'))
plt.title('Boxplot grouped by gender')
plt.xlabel('[gender]')
plt.ylabel('mean_scores')
plt.show()

 创建一个18*9的大图,使用2个子图,左边绘制每种性别(gender)的三门成绩的各自平均分(math score、reading score、writing score)的柱形图,右边绘制性别(gender)分布的饼图(分别使用(1,2,1)和(1,2,2)) 

# 2)
fig=plt.figure(figsize = (18,9))
data2 = data.groupby('gender')[['math score', 'reading score', 'writing score']].mean()

ax1=fig.add_subplot(1,2,1)
data2.plot(kind='bar',legend = False,ax = ax1)
plt.xlabel('gender')

data3 = data['gender'].value_counts()
ax2=fig.add_subplot(1,2,2)
data3.plot(kind='pie',ax = ax2)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值