gpt4 book ai didi

python - 创建 M 列的列表

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:13 24 4
gpt4 key购买 nike

我有这个代码:

cleanData = cleanData.sort_values("Name")
FinalGrade =computeFinalGrades(cleanData)
studentList = cleanData["Studentid"].tolist()
nameList = cleanData["Name"].tolist()
gradelist1 = cleanData["Assignment1"].tolist()
gradelist2 = cleanData["Assignment2"].tolist()
gradelist3 = cleanData["Assignment3"].tolist()

for i in range(len(studentList)):
print(studentList[i], " ", nameList[i], ": ",gradelist1[i], ", ", gradelist2[i], ", ", gradelist3[i], ", ", FinalGrade[i])

它给了我这个输出:

You have chosen to the show grade list for your file's data.
StudentID Name Final grade
s126519 Alberte Olsen : 2.0 , 12.0 , 0.0 , 7.0
s123333 Alexander Hansen : 7.0 , 12.0 , nan , 12.0
s123789 Bettina Petersen : 12.0 , 10.0 , 10.0 , 12.0
s128348 Ewan McGregor : 12.0 , nan , nan , 12.0
s126734 Jepser Jespersen : nan , nan , nan , -3.0
s121042 Josephine Brandt : 12.0 , 12.0 , nan , 12.0
s123235 Katinka Damgaard : 7.0 , 7.0 , 7.0 , 7.0
s127110 Lise Christiansen : -3.0 , -3.0 , -3.0 , -3.0
s123579 Marie Hansen : 10.0 , 12.0 , nan , 12.0
s123456 Michael Andersen : 7.0 , 7.0 , 4.0 , 7.0
s124444 Nanna Nygaard : 10.0 , 4.0 , 4.0 , 7.0
s121234 Natalie Sørensen : 4.0 , 10.0 , nan , 10.0
s128190 Sara Poulsen : 12.0 , 12.0 , 12.0 , 12.0
s127698 Sebastian Bruun : 7.0 , 10.0 , 10.0 , 10.0
s123468 Thomas Nielsen : -3.0 , 7.0 , 2.0 , -3.0

它列出了学生 ID、姓名、三项作业的成绩以及最终成绩。但是我如何对其进行编程,以便它能够处理 M 数量的作业,这样如果有 5000 个作业,我就不必编写 Gradelist5000 ?还有如何对齐数字使其看起来更漂亮?

最佳答案

对于您指定的任务,无需显式迭代行。也不需要将每个系列转换为列表。只需指定列并使用print即可。

首先将 FinalGrade 作为一个系列添加到您的数据框中。然后通过序列解包(* 运算符)选择任意数量的 'Assignment' 列:

cleanData['FinalGrade'] = computeFinalGrades(cleanData)
assignment_cols = [f'Assignment{i}' for i in range(1, 501)]
print(cleanData[['Studentid', 'Name', *assignment_cols, 'FinalGrade']])

关于python - 创建 M 列的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com