1. 程式人生 > 資料庫 >Python實現Mysql資料統計及numpy統計函式

Python實現Mysql資料統計及numpy統計函式

Python實現Mysql資料統計的例項程式碼如下所示:

import pymysql
import xlwt
excel=xlwt.Workbook(encoding='utf-8')
sheet=excel.add_sheet('Mysql資料庫')
sheet.write(0,'庫名')
sheet.write(0,1,'表名')
sheet.write(0,2,'資料條數')
db=pymysql.connect('192.168.1.74','root','123456','xx1')
cursor=db.cursor()
sql="select TABLE_SCHEMA as 'database',TABLE_NAME as table_name from information_schema.TABLES where TABLE_SCHEMA in ('my1','my2','t1','xx1');"
i=0
try:
 cursor.execute(sql)
 res1=cursor.fetchall()
 for row in res1:
 database=row[0]
 table=row[1]
 c1=row[0]+'.'+row[1]
 c2='select count(*) from %s'%c1
 try:
  cursor.execute(c2)
  res2=cursor.fetchall()
  for num in res2:
  count=num[0]
  i=i+1
  sheet.write(i,database)
  sheet.write(i,table)
  sheet.write(i,count)
 except:
  print('Error,Please check your code')
except:
 print('Error,Please check your code')
excel.save('C:\\Users\\user\\DeskTop\\mysql.xls')
db.close()

PS:下面看下Python資料分析numpy統計函式

np.mean(x [,axis]):
所有元素的平均值,引數是 number 或 ndarray
np.sum(x [,axis]):
所有元素的和,引數是 number 或 ndarray
np.max(x [,axis]):
所有元素的最大值,引數是 number 或 ndarray
np.min(x [,axis]):
所有元素的最小值,引數是 number 或 ndarray
np.std(x [,axis]):
所有元素的標準差,引數是 number 或 ndarray
np.var(x [,axis]):
所有元素的方差,引數是 number 或 ndarray

np.argmax(x [,axis]):
最大值的下標索引值,引數是 number 或 ndarray
np.argmin(x [,axis]):
最小值的下標索引值,引數是 number 或 ndarray
np.cumsum(x [,axis]):
返回一個同緯度陣列,每個元素都是之前所有元素的 累加和,引數是 number 或 ndarray
np.cumprod(x [,axis]):
返回一個同緯度陣列,每個元素都是之前所有元素的 累乘積,引數是 number 或 ndarray

總結

以上所述是小編給大家介紹的Python實現Mysql資料統計及numpy統計函式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!

如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!