import pandas as pd if __name__ == '__main__': student = pd.read_excel("C:/Users/18124/Desktop/pandas/019_求和_求平均_统计引导/Students.xlsx", engine="openpyxl") print(student) # 求和 求平均值 row_sum = student[["Test_1", "Test_2", "Test_3"]].sum(axis=1) # 从左到右 axis=1 row_mean = student[["Test_1", "Test_2", "Test_3"]].mean(axis=1) # 从左到右 axis=1 student["Total"] = row_sum student["Average"] = row_mean print(student) # 添加一列 col_mean = student[["Test_1", "Test_2", "Test_3", "Total", "Average"]].mean() # 求平均值 col_mean["Name"] = "Summary" # 增加名称 student = student.append(col_mean, ignore_index = True) # 添加一列 print(student) student.set_index("ID", inplace = True) print(student) student.to_excel("C:/Users/18124/Desktop/output.xlsx")