将给定目录(课程成绩)中每一个excel表格中的总成绩读取出来,然后分段显示>=90、>=80 and <90、>=70 and <80、>=60 and <70、<60 and >=0的学生人数以及在班级人数当中的比例。
时间: 2023-12-07 20:03:06 浏览: 31
好的,这是一个比较复杂的问题,需要用到Python的pandas和os库。以下是代码实现:
```python
import pandas as pd
import os
# 定义分段区间
ranges = ['>=90', '>=80 and <90', '>=70 and <80', '>=60 and <70', '<60 and >=0']
# 获取目录下的所有文件
files = os.listdir('课程成绩')
# 初始化每个区间的人数为0
counts = [0] * len(ranges)
# 统计总人数
total_count = 0
# 遍历每个文件
for file in files:
if file.endswith('.xlsx'):
# 读取excel文件
df = pd.read_excel(os.path.join('课程成绩', file))
# 统计总人数
total_count += len(df)
# 统计每个区间的人数
for i in range(len(ranges)):
if i == 0:
counts[i] += len(df[df['总成绩'] >= 90])
elif i == len(ranges) - 1:
counts[i] += len(df[df['总成绩'] < 60])
else:
counts[i] += len(df[(df['总成绩'] >= (len(ranges) - i) * 10) & (df['总成绩'] < (len(ranges) - i + 1) * 10)])
# 输出每个区间的人数和比例
for i in range(len(ranges)):
print('{}: {}人,占比{:.2%}'.format(ranges[i], counts[i], counts[i] / total_count))
```
注意,该代码需要将要处理的excel文件放在一个名为“课程成绩”的文件夹中,并且每个excel文件的表格中需要有一个名为“总成绩”的列。运行后,程序会输出每个区间的人数和比例。