程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

pandas 求和,求平均,統計

編輯:Python

有這樣一張表:

求和,求平均:

  1. import pandas as pd
  2. student = pd.read_excel("C:/Users/Administrator/Desktop/Students.xlsx",index_col="ID")
  3. temp = student[["Test_1","Test_2","Test_3"]]
  4. student["total"] = temp.sum(axis=1)#axis 0為列,1為行
  5. student["avg"] = temp.mean(axis=1)
  6. print(student)

算各科成績平均,求和:

  1. col_mean = student[["Test_1","Test_2","Test_3","total","avg"]].mean()
  2. col_mean["Name"]="Summary"
  3. student = student.append(col_mean,ignore_index=True)
  4. student[["Test_1","Test_2","Test_3","total","avg"]] = student[["Test_1","Test_2","Test_3","total","avg"]].astype(int)
  5. print(student)

append可直接加入一行

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

參考官網


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved