用Python pandas实现学生信息管理系统
import pandas as pd
file_add = r'e:\code\test\student_imformation.xlsx'
def query(col,str):
df = pd.read_excel(file_add)
imfor = df.loc[df[col]==str]
if imfor.empty :
return "没有该内容 , 请重新输入"
else:
return imfor
def delete(str):
df = pd.read_excel(file_add)
imfor = df.loc[df['学号'] == str]
if imfor.empty :
print("没有该内容 , 请重新输入")
else:
index = imfor.index
df.drop(index,inplace=True)
df.to_excel(file_add,index=0)
def alter(num,col,str):
df = pd.read_excel(file_add)
imfor = df.loc[df['学号'] == num]
if imfor.empty:
print("没有该内容 , 请重新输入")
else:
index = imfor.index
df.loc[index,col] = str
df.to_excel(file_add,index=0)
def add(str1,str2,str3,str4,str5):
df = pd.read_excel(file_add)
imfor = df.loc[df['学号'] == str1]
if imfor.empty:
a = {'学号':str1,"姓名":str2,'性别':str3,'年龄':str4,'籍贯':str5}
df = df.append(a,ignore_index=True)
ordering(df)
df.to_excel(file_add,index=0)
print("添加成功!")
else:
print("该学号已存在,请重新输入")
def ordering(df):
df.sort_values("学号", inplace=True)
def print_all():
df = pd.read_excel(file_add)
print(df)
while(True):
try :
while(True):
print("学生系统菜单: \n 1.查询 \n 2.修改 \n 3.删除 \n 4.添加 ")
option1 = int(input("\n 输入你要执行的选项:"))
if(option1 == 1):
option11 = int(input("\n 1.学号 2.姓名 3.性别 4.年龄 5.籍贯 6.打印所有\n 输入你要查询的选项:"))
if(option11 == 1):
option12 = int(input("学号:"))
print(query('学号',option12))
elif(option11 == 2):
option12 = input("姓名:")
print(query('姓名',option12))
elif(option11 == 3):
option12 = input("性别:")
print(query('性别',option12))
elif(option11 == 4):
option12 = int(input("年龄:"))
print(query('年龄', option12))
elif(option11 == 5):
option12 = input("籍贯:")
print(query('籍贯', option12))
elif (option11 == 6):
print_all()
else:
print("\n输入错误,没有该选项")
elif(option1 == 2):
option21 = int(input("输入你要修改对象的学号:"))
option22 = int(input("\n 1.姓名 2.性别 3.年龄 4.籍贯 \n 输入你要修改的内容:"))
if(option22 == 1):
option23 = input("输入修改后的姓名:")
alter(option21,"姓名",option23)
elif (option22 == 2):
option23 = input("输入修改后的性别:")
alter(option21,"性别",option23)
elif (option22 == 3):
option23 = int(input("输入修改后的年龄:"))
alter(option21,"年龄",option23)
elif (option22 == 4):
option23 = input("输入修改后的籍贯:")
alter(option21,"籍贯",option23)
else:
print("\n输入错误,没有该选项")
elif(option1 == 3):
option31 = int(input("输入你要删除对象的学号:"))
delete(option31)
elif (option1 == 4):
option41 = int(input("输入你要添加对象的学号:"))
option42 = input("输入你要添加对象的姓名:")
option43 = input("输入你要添加对象的性别:")
option44 = int(input("输入你要添加对象的年龄:"))
option45 = input("输入你要添加对象的籍贯:")
add(option41,option42,option43,option44,option45)
else:
print("\n输入错误,没有该选项")
except ValueError:
print("\n输入错误,没有该选项请重新输入")