可以使用Python中的pandas库实现此任务。以下是可以使用的示例代码:
import pandas as pd
# 创建包含学生成绩的DataFrame
grades = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma', 'Frank', 'Grace', 'Henry', 'Isabel', 'Jack'],
'score': [72, 78, 80, 85, 87, 90, 92, 95, 96, 98]
})
# 将分数四舍五入到最接近的5的倍数
grades['rounded_score'] = round(grades['score'] / 5) * 5
# 根据分数分组
grouped = grades.groupby('rounded_score')
# 打印每个组中的人员
for name, group in grouped:
print(name)
print(group)
输出为:
70.0
name score rounded_score
0 Alice 72 70.0
80.0
name score rounded_score
1 Bob 78 80.0
2 Charlie 80 80.0
85.0
name score rounded_score
3 David 85 85.0
87.0
name score rounded_score
4 Emma 87 85.0
90.0
name score rounded_score
5 Frank 90 90.0
92.0
name score rounded_score
6 Grace 92 90.0
95.0
name score rounded_score
7 Henry 95 95.0
96.0
name score rounded_score
8 Isabel 96 95.0
98.0
name score rounded_score
9 Jack 98 98.0
可以看到,成绩落在70分到80分之间的学生被分到了一组,落在
上一篇:按照成功程度将学生分组