2020-09-03

tech2024-01-04  75

第十讲练习

练习1

stu_names = ["胡歌","林更新","金世佳","丑娟"] courses = ['语文', '数学', '英语', 'Python', '体育'] data_1 = np.array([[87., 74., 98., 84., np.nan],[79., 69., 61., 99., np.nan],[84., 84., 94., 66., np.nan],[90., 60., 72., 90., np.nan]]) df = pd.DataFrame(data=data_1,index=stu_names,columns=courses,dtype=None,copy=False) df.to_excel("学生成绩表.xlsx") df ele_names = ["胡歌","林更新","金世佳","丑娟"] ele_courses = ['统计学', '日语'] ele_data = np.array([[85., np.nan],[np.nan, 69.],[np.nan, 95.],[80., np.nan]]) df2 = pd.DataFrame(data=ele_data,index=ele_names,columns=ele_courses,dtype=None,copy=False) df2.to_excel("选修成绩表.xlsx") df2

练习2

# pd.read_excel?? data1 = pd.read_excel("学生成绩表.xlsx",sheet_name=0,header=0,index_col=0) data1 data2 = pd.read_excel("选修成绩表.xlsx",index_col=0) data2

练习3

# del df['体育']#del方法,影响原数据 df = df.drop(columns='体育')#drop方法,不影响原数据 df

#方法一,按列添加 df['统计学'] = [85.0,np.NaN,np.NaN,80.0] df['日语'] = [np.NaN,69.0,95.0,np.NaN] #方法二,插入列 # del df['{}'.format(i) for i in['统计学','日语']]#错误 del df['日语'] del df['统计学'] del df['总成绩'] df.insert(4,'统计学',[85.0,np.NaN,np.NaN,80.0]) df.insert(5,'日语',[np.NaN,69.0,95.0,np.NaN]) #总成绩 df5 = df.sum(axis=1) df['总成绩'] = df5 df.to_excel("总表.xlsx") df

最新回复(0)