知识结构:
- 利用pandas构建一个成绩单
- 利用apply函数进行条件统计
- 把数据进行表单保存
源代码如下:
import numpy as np
import pandas as pd
a = np.arange(10,200)
a_b = []
a_c = []
for i in range(10):
b = np.random.randint(120)
c=i+b
a_b.append(c)
a_c.append(np.random.randint(120))
print(i+b)
data = {"语文":a_b,"数学":a_c}
index=["张三","李四","王小丫","刘备","关羽","张飞","张三","刘一","张军","赵明",]
df = pd.DataFrame(data,index=index)
def get_score(df):
if df["语文"] >80:
return "优秀"
if df["语文"]>60:
return "及格"
if df["语文"]<60:
return "不及格"
df.loc[:,"语文水平"]=df.apply(get_score,axis=1)
def get_score(df):
if df["数学"] >80:
return "优秀"
if df["数学"]>60:
return "及格"
if df["数学"]<60:
return "不及格"
"""
1 、df.loc[:,"新列名称"]
2、执行函数 df.apply(函数名,竖直方向)
"""
df.loc[:,"数学水平"]=df.apply(get_score,axis=1)
df_des = df.describe()
writer = pd.ExcelWriter("语文数学成绩单.xlsx")
df.to_excel(writer,"成绩单")
df_des.to_excel(writer,"成绩单分析")
writer.save()