利用Python统计excel表格

一、题目描述
在这里插入图片描述
二、代码展示

import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
Duty=pd.read_csv('E:\\student_duty.csv',encoding='GBK')
Score=pd.read_csv('E:\\student_score1.csv',encoding='GBK')
Duty1=pd.DataFrame(Duty)
Score1=pd.DataFrame(Score)
sum=Score1.数学+Score1.英语+Score1.Python+Score1.通信技术 #求解sum
Score['总分']=sum #吧sum添加到总分中
Score2=Score.sort_values(by=['总分'],ascending=False) #排序
c=Score1.groupby('总分')
for name,group in Score1.groupby(Score1['性别']): #根据性别分组
    print(name)
    print(group)
    print('平均分:',group['总分'].sum()/group.shape[0])
    print('最高分:',group['总分'].max(),'\n')
a=list(sum) #将sum转换成list
print(a[:]) #用索引读取a的值看是否正确
Score1['等级']=None #添加Score时必须赋值
for i in range(len(a)):
   if (a[i]>320):
       Score1.loc[i,'等级']='A'
   elif (250<=a[i]<=320):
       Score1.loc[i,'等级']='B'
   else :
       Score1.loc[i,'等级']='C'
print(Score1)
Students=pd.merge(Score,Duty,how='left',on='学号') #合并两个表格
print(Students)
Students.to_excel('E:\\Students.xlsx')       #保存到E盘中

三、需要到的表格
链接:表格及源代码下载
提取码:8h4t
(注:本实验.csv表格是基于E盘目录下读取的)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章