有這樣一張表:
求和,求平均:
import pandas as pd
student = pd.read_excel("C:/Users/Administrator/Desktop/Students.xlsx",index_col="ID")
temp = student[["Test_1","Test_2","Test_3"]]
student["total"] = temp.sum(axis=1)#axis 0為列,1為行
student["avg"] = temp.mean(axis=1)
print(student)
算各科成績平均,求和:
col_mean = student[["Test_1","Test_2","Test_3","total","avg"]].mean()
col_mean["Name"]="Summary"
student = student.append(col_mean,ignore_index=True)
student[["Test_1","Test_2","Test_3","total","avg"]] = student[["Test_1","Test_2","Test_3","total","avg"]].astype(int)
print(student)
append可直接加入一行
DataFrame.
append
(other, ignore_index=False, verify_integrity=False, sort=None)
參考官網