产生50个班、每个班50名同学的五门课程成绩数据,要求有“姓名、高等数学、大学英语、操作系统、Python语言、计算机组成原理”这些字段,将生成的数据存入“class.xlsx”文件中,工作表的名称为“1班”到“50班”。用numpy、pandas和python编程实现。
时间: 2025-01-29 07:16:06 浏览: 28
要在Python中使用numpy、pandas库生成并保存这样的数据,你可以按照以下步骤操作:
```python
import numpy as np
import pandas as pd
# 设置随机种子以便复现结果
np.random.seed(42)
# 创建学生信息列表
students_list = []
for i in range(1, 51): # 从1班到50班
students_in_class = []
for _ in range(50): # 每班50名同学
name = 'Student_{}'.format(i * 50 + _) # 假设姓名为连续编号
scores = {
'姓名': name,
'高等数学': np.random.randint(60, 101),
'大学英语': np.random.randint(60, 101),
'操作系统': np.random.randint(60, 101),
'Python语言': np.random.randint(60, 101),
'计算机组成原理': np.random.randint(60, 101)
}
students_in_class.append(scores)
students_list.append(students_in_class)
# 将所有班级的学生信息合并成一个DataFrame
all_students_df = pd.concat(students_list)
# 存储至Excel文件
filename = 'class.xlsx'
sheet_names = list(range(1, 51)) # 工作表名称为“1班”到“50班”
all_students_df.to_excel(filename, index=False, sheet_name=sheet_names)
print("数据已成功保存到 'class.xlsx' 文件中.")
阅读全文
相关推荐

















