numpy和Pandas用法講解


  1 一。 數組要比列表效率高很多
  2     numpy高效的處理數據,提供數組的支持,python默認沒有數組。pandas、scipy、matplotlib都依賴numpy。
  3     pandas主要用於數據挖掘,探索,分析
  4     maiplotlib用於作圖,可視化
  5     scipy進行數值計算,如:積分,傅里葉變換,微積分
  6     statsmodels用於統計分析
  7     Gensim用於文本挖掘
  8     sklearn機器學習, keras深度學習
  9 二。
 10     numpy和mkl 下載安裝
 11     pandas和maiplotlib網絡安裝
 12     scipy 下載安裝
 13     statsmodels和Gensim網絡安裝
 14 三numpy的操作。
 15     import numpy
 16     # 創建數一維數組組
 17     # numpy.array([元素1,元素2,......元素n])
 18     x = numpy.array(['a', '9', '8', '1'])
 19     # 創建二維數組格式
 20     # numpy.array([[元素1,元素2,......元素n],[元素1,元素2,......元素n],[元素1,元素2,......元素n]])
 21     y = numpy.array([[3,5,7],[9,2,6],[5,3,0]])
 22     # 排序
 23     x.sort()
 24     y.sort()
 25     # 取最大值
 26     y1 = y.max()
 27     # 取最小值
 28     y2 = y.main()
 29     # 切片
 30 四pandas的操作。
 31     import pandas as pda
 32     # 使用pandas生成數據
 33     # Series代表某一串數據 index指定行索引名稱,Series索引默認從零開始
 34     # DataFrame代表行列整合出來的數據框,columns 指定列名
 35     a = pda.Series([8, 9, 2, 1], index=['one', 'two', 'three', 'four'])
 36     # 以列表的格式創建數據框
 37     b = pda.DataFrame([[5,6,2,3],[3,5,1,4],[7,9,3,5]], columns=['one', 'two', 'three', 'four'],index=['one', 'two', 'three'])
 38     # 以字典的格式創建數據框
 39     c = pda.DataFrame({
 40         'one':4, # 會自動補全
 41         'two':[6,2,3],
 42         'three':list(str(982))
 43     })
 44     # b.head(行數)# 默認取前5行頭
 45     # b.tail(行數)# 默認取后5行尾
 46     # b.describe() 統計數據的情況  count mean std min 25% max
 47     e = b.head()
 48     f = b.describe()
 49     # 數據的轉置,及行變成列,列變成行
 50     g = b.T
 51 五python數據的導入
 52     import pandas as pad
 53     f = open('d:/大.csv','rb')
 54     # 導入csv
 55     a = pad.read_csv(f, encoding='python')
 56     # 顯示多少行多少列
 57     a.shape()
 58     a.values[0][2] #第一行第三列
 59     # 描述csv數據
 60     b = a.describe()
 61     # 排序
 62     c = a.sort_values()
 63     # 導入excel
 64     d = pad.read_excel('d:/大.xls')
 65     print(d)
 66     print(d.describe())
 67     # 導入mysql
 68     import pymysql
 69     conn = pymysql.connect(host='localhost', user='root', passwd='root', db='')
 70     sql = 'select * from mydb'
 71     e = pad.read_sql(sql, conn)
 72     # 導入html表格數據 需要先安裝 html5lib和bs4
 73     g = pad.read_html('https://book.douban.com/subject/30258976/?icn=index-editionrecommend')
 74     # 導入文本數據
 75     h = pad.read_table('d:/lianjie.txt','rb', engine='python')
 76     print(h.describe())
 77 六matplotlib的使用
 78     # 折線圖/散點圖用plot
 79     # 直方圖用hist
 80     import matplotlib.pylab as pyl
 81     import numpy as npy
 82     x = [1,2,4,6,8,9]
 83     y = [5,6,7,8,9,0]
 84     pyl.plot(x, y) #plot(x軸數據,y軸數據,展現形式)
 85     # o散點圖,默認是直線 c cyan青色 r red紅色 m magente品紅色 g green綠色 b blue藍色 y yellow黃色 w white白色
 86     # -直線  --虛線  -. -.形式  :細小虛線
 87     # s方形 h六角形  *星星  + 加號  x x形式 d菱形 p五角星
 88     pyl.plot(x, y, 'D')
 89     pyl.title('name') #名稱
 90     pyl.xlabel('xname') #x軸名稱
 91     pyl.ylabel('yname') #y軸名稱
 92     pyl.xlim(0,20) #設置x軸的范圍
 93     pyl.ylim(2,22) #設置y軸的范圍
 94     pyl.show()
 95     # 隨機數的生成
 96     data = npy.random.random_integers(1,20,100) #(最小值,最大值,個數)
 97     # 生成具有正態分布的隨機數
 98     data2 = npy.random.normal(10.0, 1.0, 10000) #(均值,西格瑪,個數)
 99     # 直方圖hist
100     pyl.hist(data)
101     pyl.hist(data2)
102     # 設置直方圖的上限下限
103     sty = npy.arange(2,20,2) #步長也表示直方圖的寬度
104     pyl.hist(data, sty, histtype='stepfilled') # 去除輪廓
105     # 子圖的繪制和使用
106     pyl.subplot(2, 2, 2) # (行,列,當前區域)
107     x1 = [2,3,5,8,6,7]
108     y1 = [2,3,5,9,6,7]
109     pyl.plot(x1, y1)
110     pyl.subplot(2, 2, 1) # (行,列,當前區域)
111     x1 = [2,3,5,9,6,7]
112     y1 = [2,3,5,9,6,7]
113     pyl.plot(x1, y1)
114     pyl.subplot(2, 1, 2) # (行,列,當前區域)
115     x1 = [2,3,5,9,6,7]
116     y1 = [2,3,9,5,6,7]
117     pyl.plot(x1, y1)
118     pyl.show()

另外安利一個學習教程:Python3數據科學入門與實戰(視頻+源碼)

分享一個好的IT資源平台:點擊進入


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM