import pandas as pd
#识别登分表中含有的科目
data = pd.read_excel(r'D:\Desktop\登分表.xls')
columns = list(data.columns)
subjects_list = ['语文','数学','英语','日语','外语','物理','化学','地理','生物','政治','历史']
subjects = [i for i in columns if i in subjects_list]
#新增已有科目的单科排名sheet
with pd.ExcelWriter(r'D:\Desktop\成绩统计.xls') as writer:
data.to_excel(writer, sheet_name='所有科目')
Personal_Information = columns.index(subjects[1])-1
for i in subjects:
rank = i +'单科排名'
single_subject_ranking = data.loc[:,i]
single_subject_ranking.sort_values(rank,inplace = True)
single_subject_ranking.to_excel(writer,sheet = rank)
sequence = []
for i in subjects:
data[i + '排名'] = data[i].rank(method='min', ascending=False)
sequence.append(i,i+'排名')
data['三科总分'] = data['语文'] + data['数学'] + data['英语']
data['三科总分排名'] = data['三科总分'].rank(method = 'min',ascending = False)
data[range(subjects) + '科总分'] = data.loc[Personal_Information:].sum(axis=1)
data[range(subjects) + '科总分名次'] = data[range(subjects)].rank(method = 'min',ascending = False)
data.sort_values(by= range(subjects) + "科总分名次", inplace=True)
如上
本帖最后由 来自星星的小明 于 2021-2-18 14:46 编辑
rank = i +'单科排名'
single_subject_ranking.sort_values(rank,inplace = True)
rank 是你自己新定义的字段,原数据并不存在这一列,不能根据rank字段排序
sort_values()
by:str or list of str;如果axis=0,那么by="列名";如果axis=1,那么by="行名";
single_subject_ranking = data.loc[:,i]
这句代码只是拿到了这个DataFrame中的数据,没有表头,是根据表头的字段对应的列进行排序的
|