一、題目描述
二、代碼展示
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盤目錄下讀取的)