1、爲何要使用Numpy and Pandas?html
運算速度快:numpy 和 pandas 都是採用 C 語言編寫, pandas 又是基於 numpy, 是 numpy 的升級版本。python
消耗資源少:採用的是矩陣運算,會比 python 自帶的字典或者列表快好多。json
numpy 和 pandas是科學計算中很重要的兩個模塊,能夠應用於數據分析,機器學習和深度學習數組
2、Numpy 和 Pandas 安裝數據結構
能夠經過sudo pip install numpy和sudo pip install pandas進行安裝。app
3、Numpy 的學習dom
ndim:維度機器學習
shape:行數和列數函數
size:元素個數學習
①、numpy屬性:
1 import numpy as np #爲了方便使用numpy 採用np簡寫 2 3 array = np.array([[1,2,3],[2,3,4]]) #列表轉化爲矩陣 4 print(array) 5 6 print('number of dim:',array.ndim) # 維度 7 # number of dim: 2 8 9 print('shape :',array.shape) # 行數和列數 10 # shape : (2, 3) 11 12 print('size:',array.size) # 元素個數 13 # size: 6
②、numpy建立array
關鍵字:array:建立數組
dtype:指定數據類型
zeros:建立數據全爲0
ones:建立數據全爲1
empty:建立數據接近0
arrange:按指定範圍建立數據
linspace:建立線段
建立數組:
1 a = np.array([2,23,4]) # list 1d 2 print(a) 3 # [2 23 4]
指定數據 dtype:
1 a = np.array([2,23,4],dtype=np.int) 2 print(a.dtype) 3 # int 64 4 5 a = np.array([2,23,4],dtype=np.int32) 6 print(a.dtype) 7 # int32 8 9 a = np.array([2,23,4],dtype=np.float) 10 print(a.dtype) 11 # float64 12 13 a = np.array([2,23,4],dtype=np.float32) 14 print(a.dtype) 15 # float32
建立特定數據:
1 a = np.array([[2,23,4],[2,32,4]]) # 2d 矩陣 2行3列 2 print(a) 3 """ 4 [[ 2 23 4] 5 [ 2 32 4]] 6 """ 7 8 #建立全零數組 9 a = np.zeros((3,4)) # 數據全爲0,3行4列 10 """ 11 array([[ 0., 0., 0., 0.], 12 [ 0., 0., 0., 0.], 13 [ 0., 0., 0., 0.]]) 14 """ 15 16 #建立全一數組, 同時也能指定這些特定數據的 dtype: 17 a = np.ones((3,4),dtype = np.int) # 數據爲1,3行4列 18 """ 19 array([[1, 1, 1, 1], 20 [1, 1, 1, 1], 21 [1, 1, 1, 1]]) 22 """ 23 24 #建立全空數組, 其實每一個值都是接近於零的數: 25 a = np.empty((3,4)) # 數據爲empty,3行4列 26 """ 27 array([[ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324, 28 1.48219694e-323], 29 [ 1.97626258e-323, 2.47032823e-323, 2.96439388e-323, 30 3.45845952e-323], 31 [ 3.95252517e-323, 4.44659081e-323, 4.94065646e-323, 32 5.43472210e-323]]) 33 """ 34 35 #用 arange 建立連續數組: 36 a = np.arange(10,20,2) # 10-19 的數據,2步長 37 """ 38 array([10, 12, 14, 16, 18]) 39 """ 40 41 #使用 reshape 改變數據的形狀 42 a = np.arange(12).reshape((3,4)) # 3行4列,0到11 43 """ 44 array([[ 0, 1, 2, 3], 45 [ 4, 5, 6, 7], 46 [ 8, 9, 10, 11]]) 47 """ 48 49 #用 linspace 建立線段型數據: 50 a = np.linspace(1,10,20) # 開始端1,結束端10,且分割成20個數據,生成線段 51 """ 52 array([ 1. , 1.47368421, 1.94736842, 2.42105263, 53 2.89473684, 3.36842105, 3.84210526, 4.31578947, 54 4.78947368, 5.26315789, 5.73684211, 6.21052632, 55 6.68421053, 7.15789474, 7.63157895, 8.10526316, 56 8.57894737, 9.05263158, 9.52631579, 10. ]) 57 """ 58 59 #進行 reshape 工做: 60 a = np.linspace(1,10,20).reshape((5,4)) # 更改shape 61 """ 62 array([[ 1. , 1.47368421, 1.94736842, 2.42105263], 63 [ 2.89473684, 3.36842105, 3.84210526, 4.31578947], 64 [ 4.78947368, 5.26315789, 5.73684211, 6.21052632], 65 [ 6.68421053, 7.15789474, 7.63157895, 8.10526316], 66 [ 8.57894737, 9.05263158, 9.52631579, 10. ]]) 67 """
③、numpy的基礎運算1:
1 import numpy as np 2 a=np.array([10,20,30,40]) # array([10, 20, 30, 40]) 3 b=np.arange(4) # array([0, 1, 2, 3]) 4 5 #numpy 的幾種基本運算: 6 c=a-b # array([10, 19, 28, 37]) 7 8 c=a+b # array([10, 21, 32, 43]) 9 10 c=a*b # array([ 0, 20, 60, 120]) 11 12 c=b**2 # array([0, 1, 4, 9]) 13 14 c=10*np.sin(a) 15 # array([-5.44021111, 9.12945251, -9.88031624, 7.4511316 ]) 16 17 print(b<3) 18 # array([ True, True, True, False], dtype=bool) 19 20 #對多行多維度的矩陣進行操做,須要對開始的腳本進行一些修改: 21 a=np.array([[1,1],[0,1]]) 22 b=np.arange(4).reshape((2,2)) 23 24 print(a) 25 # array([[1, 1], 26 # [0, 1]]) 27 28 print(b) 29 # array([[0, 1], 30 # [2, 3]]) 31 32 #矩陣乘法 33 c_dot = np.dot(a,b) 34 # array([[2, 4], 35 # [2, 3]]) 36 37 c_dot_2 = a.dot(b) 38 # array([[2, 4], 39 # [2, 3]]) 40 41 #sum(), min(), max()的使用: 42 import numpy as np 43 a=np.random.random((2,4)) 44 print(a) 45 # array([[ 0.94692159, 0.20821798, 0.35339414, 0.2805278 ], 46 # [ 0.04836775, 0.04023552, 0.44091941, 0.21665268]]) 47 48 np.sum(a) # 4.4043622002745959 49 np.min(a) # 0.23651223533671784 50 np.max(a) # 0.90438450240606416 51 52 #當axis的值爲0的時候,將會以列做爲查找單元, 當axis的值爲1的時候,將會以行做爲查找單元。 53 print("a =",a) 54 # a = [[ 0.23651224 0.41900661 0.84869417 0.46456022] 55 # [ 0.60771087 0.9043845 0.36603285 0.55746074]] 56 57 print("sum =",np.sum(a,axis=1)) 58 # sum = [ 1.96877324 2.43558896] 59 60 print("min =",np.min(a,axis=0)) 61 # min = [ 0.23651224 0.41900661 0.36603285 0.46456022] 62 63 print("max =",np.max(a,axis=1)) 64 # max = [ 0.84869417 0.9043845 ]
④、numpy 的幾種基本運算2:
1 import numpy as np 2 A = np.arange(2,14).reshape((3,4)) 3 4 # array([[ 2, 3, 4, 5] 5 # [ 6, 7, 8, 9] 6 # [10,11,12,13]]) 7 8 #最小值和最大值的索引號 9 print(np.argmin(A)) # 0 10 print(np.argmax(A)) # 11 11 12 #整個矩陣的均值 13 print(np.mean(A)) # 7.5 14 print(np.average(A)) # 7.5 15 16 #mean()函數還有另一種寫法: 17 print(A.mean()) # 7.5 18 19 #求解中位數的函數: 20 print(A.median()) # 7.5 21 22 #cumsum()累加函數 23 print(np.cumsum(A)) 24 # [2 5 9 14 20 27 35 44 54 65 77 90] 25 26 #累差運算函數:該函數計算的即是每一行中後一項與前一項之差。故一個3行4列矩陣經過函數計算獲得的矩陣即是3行3列的矩陣。 27 print(np.diff(A)) 28 29 # [[1 1 1] 30 # [1 1 1] 31 # [1 1 1]] 32 33 #nonzero()函數:將全部非零元素的行與列座標分割開,重構成兩個分別關於行和列的矩陣。 34 print(np.nonzero(A)) 35 36 # (array([0,0,0,0,1,1,1,1,2,2,2,2]),array([0,1,2,3,0,1,2,3,0,1,2,3])) 37 38 #排序函數仍然僅針對每一行進行從小到大排序操做: 39 import numpy as np 40 A = np.arange(14,2, -1).reshape((3,4)) 41 42 # array([[14, 13, 12, 11], 43 # [10, 9, 8, 7], 44 # [ 6, 5, 4, 3]]) 45 46 print(np.sort(A)) 47 48 # array([[11,12,13,14] 49 # [ 7, 8, 9,10] 50 # [ 3, 4, 5, 6]]) 51 52 #矩陣的轉置有兩種表示方法: 53 print(np.transpose(A)) 54 print(A.T) 55 56 # array([[14,10, 6] 57 # [13, 9, 5] 58 # [12, 8, 4] 59 # [11, 7, 3]]) 60 # array([[14,10, 6] 61 # [13, 9, 5] 62 # [12, 8, 4] 63 # [11, 7, 3]]) 64 65 #函數的格式是clip(Array,Array_min,Array_max),顧名思義,Array指的是將要被執行用的矩陣,然後面的最小值最大值則用於讓函數判斷矩陣中元素是否有比最小值小的或者比最大值大的元素,並將這些指定的元素轉換爲最小值或者最大值。 66 print(A) 67 # array([[14,13,12,11] 68 # [10, 9, 8, 7] 69 # [ 6, 5, 4, 3]]) 70 71 print(np.clip(A,5,9)) 72 # array([[ 9, 9, 9, 9] 73 # [ 9, 9, 8, 7] 74 # [ 6, 5, 5, 5]])
⑤、Numpy 索引
1 #一維索引 2 import numpy as np 3 A = np.arange(3,15) 4 5 # array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) 6 7 print(A[3]) # 6 8 9 #將矩陣轉換爲二維的,此時進行一樣的操做: 10 #A[2]對應的就是矩陣A中第三行(從0開始算第一行)的全部元素。 11 A = np.arange(3,15).reshape((3,4)) 12 """ 13 array([[ 3, 4, 5, 6] 14 [ 7, 8, 9, 10] 15 [11, 12, 13, 14]]) 16 """ 17 18 print(A[2]) 19 # [11 12 13 14]
1 A = np.arange(3,15).reshape((3,4)) 2 """ 3 array([[ 3, 4, 5, 6] 4 [ 7, 8, 9, 10] 5 [11, 12, 13, 14]]) 6 """ 7 print(A[1][1]) # 8 8 print(A[1, 1:3]) # [8 9] 9 10 #利用for函數進行打印: 11 for row in A: 12 print(row) 13 """ 14 [ 3, 4, 5, 6] 15 [ 7, 8, 9, 10] 16 [11, 12, 13, 14] 17 """ 18 19 #進行逐列打印 20 for column in A.T: 21 print(column) 22 """ 23 [ 3, 7, 11] 24 [ 4, 8, 12] 25 [ 5, 9, 13] 26 [ 6, 10, 14] 27 """ 28 29 #flatten是一個展開性質的函數,將多維的矩陣進行展開成1行的數列。而flat是一個迭代器,自己是一個object屬性。 30 import numpy as np 31 A = np.arange(3,15).reshape((3,4)) 32 33 print(A.flatten()) 34 # array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) 35 36 for item in A.flat: 37 print(item) 38 39 # 3 40 # 4 41 …… 42 # 14
⑥、Numpy array 合併
1 #vertical stack自己屬於一種上下合併,即對括號中的兩個總體進行對應操做。 2 import numpy as np 3 A = np.array([1,1,1]) 4 B = np.array([2,2,2]) 5 6 print(np.vstack((A,B))) # vertical stack 7 """ 8 [[1,1,1] 9 [2,2,2]] 10 """ 11 12 #對組合而成的矩陣進行屬性探究:利用shape函數可讓咱們很容易地知道A和C的屬性,從打印出的結果來看,A僅僅是一個擁有3項元素的數組(數列),而合併後獲得的C是一個2行3列的矩陣。 13 C = np.vstack((A,B)) 14 print(A.shape,C.shape) 15 16 # (3,) (2,3) 17 18 #左右合併: 19 D = np.hstack((A,B)) # horizontal stack 20 21 print(D) 22 # [1,1,1,2,2,2] 23 24 print(A.shape,D.shape) 25 # (3,) (6,) 26 27 #array轉換爲了1行3列以及3行1列的矩陣 28 print(A[np.newaxis,:]) 29 # [[1 1 1]] 30 31 print(A[np.newaxis,:].shape) 32 # (1,3) 33 34 print(A[:,np.newaxis]) 35 """ 36 [[1] 37 [1] 38 [1]] 39 """ 40 41 print(A[:,np.newaxis].shape) 42 # (3,1) 43 44 #綜合起來: 45 import numpy as np 46 A = np.array([1,1,1])[:,np.newaxis] 47 B = np.array([2,2,2])[:,np.newaxis] 48 49 C = np.vstack((A,B)) # vertical stack 50 D = np.hstack((A,B)) # horizontal stack 51 52 print(D) 53 """ 54 [[1 2] 55 [1 2] 56 [1 2]] 57 """ 58 59 print(A.shape,D.shape) 60 # (3,1) (3,2) 61 62 #合併操做須要針對多個矩陣或序列時,藉助concatenate函數更加方便: 63 C = np.concatenate((A,B,B,A),axis=0) 64 65 print(C) 66 """ 67 array([[1], 68 [1], 69 [1], 70 [2], 71 [2], 72 [2], 73 [2], 74 [2], 75 [2], 76 [1], 77 [1], 78 [1]]) 79 """ 80 81 D = np.concatenate((A,B,B,A),axis=1) 82 83 print(D) 84 """ 85 array([[1, 2, 2, 1], 86 [1, 2, 2, 1], 87 [1, 2, 2, 1]]) 88 """
⑦、Numpy array 分割
1 #建立數據 2 import numpy as np 3 #創建3行4列的Array 4 A = np.arange(12).reshape((3, 4)) 5 print(A) 6 """ 7 array([[ 0, 1, 2, 3], 8 [ 4, 5, 6, 7], 9 [ 8, 9, 10, 11]]) 10 """ 11 12 #縱向分割 13 print(np.split(A, 2, axis=1)) 14 """ 15 [array([[0, 1], 16 [4, 5], 17 [8, 9]]), array([[ 2, 3], 18 [ 6, 7], 19 [10, 11]])] 20 """ 21 22 #橫向分割 23 print(np.split(A, 3, axis=0)) 24 25 # [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])] 26 27 #不等量的分割 ,np.array_split() 28 print(np.array_split(A, 3, axis=1)) 29 """ 30 [array([[0, 1], 31 [4, 5], 32 [8, 9]]), array([[ 2], 33 [ 6], 34 [10]]), array([[ 3], 35 [ 7], 36 [11]])] 37 """ 38 39 #其餘的分割方式 40 print(np.vsplit(A, 3)) #等於 print(np.split(A, 3, axis=0)) 41 42 # [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])] 43 44 45 print(np.hsplit(A, 2)) #等於 print(np.split(A, 2, axis=1)) 46 """ 47 [array([[0, 1], 48 [4, 5], 49 [8, 9]]), array([[ 2, 3], 50 [ 6, 7], 51 [10, 11]])] 52 """
⑦、Numpy copy & deep copy
1 #= 的賦值方式會帶有關聯性 2 import numpy as np 3 4 a = np.arange(4) 5 # array([0, 1, 2, 3]) 6 7 b = a 8 c = a 9 d = b 10 #改變a的第一個值,b、c、d的第一個值也會同時改變。 11 a[0] = 11 12 print(a) 13 # array([11, 1, 2, 3]) 14 15 #確認b、c、d是否與a相同 16 b is a # True 17 c is a # True 18 d is a # True 19 20 #一樣更改d的值,a、b、c也會改變 21 d[1:3] = [22, 33] # array([11, 22, 33, 3]) 22 print(a) # array([11, 22, 33, 3]) 23 print(b) # array([11, 22, 33, 3]) 24 print(c) # array([11, 22, 33, 3]) 25 26 #copy() 的賦值方式沒有關聯性 27 b = a.copy() # deep copy 28 print(b) # array([11, 22, 33, 3]) 29 a[3] = 44 30 print(a) # array([11, 22, 33, 44]) 31 print(b) # array([11, 22, 33, 3])
4、Pandas學習
Numpy 和 Pandas 有什麼不一樣 ?
用 python 的列表和字典來做比較, 那麼能夠說 Numpy 是列表形式的,沒有數值標籤,而 Pandas 就是字典形式。Pandas是基於Numpy構建的,讓Numpy爲中心的應用變得更加簡單。
要使用pandas,首先須要瞭解他主要兩個數據結構:Series和DataFrame。
①、pandas基本狀況
1 #Series用法 2 #Series的字符串表現形式爲:索引在左邊,值在右邊。因爲咱們沒有爲數據指定索引。因而會自動建立一個0到N-1(N爲長度)的整數型索引。 3 import pandas as pd 4 import numpy as np 5 s = pd.Series([1,3,6,np.nan,44,1]) 6 7 print(s) 8 """ 9 0 1.0 10 1 3.0 11 2 6.0 12 3 NaN 13 4 44.0 14 5 1.0 15 dtype: float64 16 """
1 #DataFrame用法 2 #DataFrame是一個表格型的數據結構,它包含有一組有序的列,每列能夠是不一樣的值類型(數值,字符串,布爾值等)。DataFrame既有行索引也有列索引, 它能夠被看作由Series組成的大字典。 3 dates = pd.date_range('20160101',periods=6) 4 df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d']) 5 6 print(df) 7 """ 8 a b c d 9 2016-01-01 -0.253065 -2.071051 -0.640515 0.613663 10 2016-01-02 -1.147178 1.532470 0.989255 -0.499761 11 2016-01-03 1.221656 -2.390171 1.862914 0.778070 12 2016-01-04 1.473877 -0.046419 0.610046 0.204672 13 2016-01-05 -1.584752 -0.700592 1.487264 -1.778293 14 2016-01-06 0.633675 -1.414157 -0.277066 -0.442545 15 """ 16 17 #DataFrame 的一些簡單運用 18 print(df['b']) 19 20 """ 21 2016-01-01 -2.071051 22 2016-01-02 1.532470 23 2016-01-03 -2.390171 24 2016-01-04 -0.046419 25 2016-01-05 -0.700592 26 2016-01-06 -1.414157 27 Freq: D, Name: b, dtype: float64 28 """ 29 30 #建立一組沒有給定行標籤和列標籤的數據 df1,默認的從0開始 index. 31 df1 = pd.DataFrame(np.arange(12).reshape((3,4))) 32 print(df1) 33 34 """ 35 0 1 2 3 36 0 0 1 2 3 37 1 4 5 6 7 38 2 8 9 10 11 39 """ 40 41 #還有一種生成 df 的方法 42 df2 = pd.DataFrame({'A' : 1., 43 'B' : pd.Timestamp('20130102'), 44 'C' : pd.Series(1,index=list(range(4)),dtype='float32'), 45 'D' : np.array([3] * 4,dtype='int32'), 46 'E' : pd.Categorical(["test","train","test","train"]), 47 'F' : 'foo'}) 48 49 print(df2) 50 51 """ 52 A B C D E F 53 0 1.0 2013-01-02 1.0 3 test foo 54 1 1.0 2013-01-02 1.0 3 train foo 55 2 1.0 2013-01-02 1.0 3 test foo 56 3 1.0 2013-01-02 1.0 3 train foo 57 """ 58 59 #查看數據中的類型 60 print(df2.dtypes) 61 62 """ 63 df2.dtypes 64 A float64 65 B datetime64[ns] 66 C float32 67 D int32 68 E category 69 F object 70 dtype: object 71 """ 72 73 #對列的序號: 74 print(df2.index) 75 76 # Int64Index([0, 1, 2, 3], dtype='int64') 77 78 #每種數據的名稱: 79 print(df2.columns) 80 81 # Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object') 82 83 84 #全部df2的值: 85 print(df2.values) 86 87 """ 88 array([[1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'], 89 [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'], 90 [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'], 91 [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']], dtype=object) 92 """ 93 94 #數據的總結, 能夠用 describe():st爲標準差 95 df2.describe() 96 97 """ 98 A C D 99 count 4.0 4.0 4.0 100 mean 1.0 1.0 3.0 101 std 0.0 0.0 0.0 102 min 1.0 1.0 3.0 103 25% 1.0 1.0 3.0 104 50% 1.0 1.0 3.0 105 75% 1.0 1.0 3.0 106 max 1.0 1.0 3.0 107 """ 108 109 #翻轉數據, transpose: 110 print(df2.T) 111 112 """ 113 0 1 2 \ 114 A 1 1 1 115 B 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00 116 C 1 1 1 117 D 3 3 3 118 E test train test 119 F foo foo foo 120 121 3 122 A 1 123 B 2013-01-02 00:00:00 124 C 1 125 D 3 126 E train 127 F foo 128 129 """ 130 131 #對數據的 index 進行排序並輸出: 132 print(df2.sort_index(axis=1, ascending=False)) 133 134 """ 135 F E D C B A 136 0 foo test 3 1.0 2013-01-02 1.0 137 1 foo train 3 1.0 2013-01-02 1.0 138 2 foo test 3 1.0 2013-01-02 1.0 139 3 foo train 3 1.0 2013-01-02 1.0 140 """ 141 142 #對數據 值 排序輸出: 143 print(df2.sort_values(by='B')) 144 145 """ 146 A B C D E F 147 0 1.0 2013-01-02 1.0 3 test foo 148 1 1.0 2013-01-02 1.0 3 train foo 149 2 1.0 2013-01-02 1.0 3 test foo 150 3 1.0 2013-01-02 1.0 3 train foo 151 """
②、Pandas 選擇數據
1 #創建了一個 6X4 的矩陣數據 2 dates = pd.date_range('20130101', periods=6) 3 df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D']) 4 5 """ 6 A B C D 7 2013-01-01 0 1 2 3 8 2013-01-02 4 5 6 7 9 2013-01-03 8 9 10 11 10 2013-01-04 12 13 14 15 11 2013-01-05 16 17 18 19 12 2013-01-06 20 21 22 23 13 """ 14 15 #簡單的篩選 16 #選取DataFrame中的數據 17 print(df['A']) 18 print(df.A) 19 20 """ 21 2013-01-01 0 22 2013-01-02 4 23 2013-01-03 8 24 2013-01-04 12 25 2013-01-05 16 26 2013-01-06 20 27 Freq: D, Name: A, dtype: int64 28 """ 29 30 #選擇跨越多行或多列: 31 print(df[0:3]) 32 33 """ 34 A B C D 35 2013-01-01 0 1 2 3 36 2013-01-02 4 5 6 7 37 2013-01-03 8 9 10 11 38 """ 39 40 print(df['20130102':'20130104']) 41 42 """ 43 A B C D 44 2013-01-02 4 5 6 7 45 2013-01-03 8 9 10 11 46 2013-01-04 12 13 14 15 47 """ 48 49 #根據標籤 loc 50 print(df.loc['20130102']) 51 """ 52 A 4 53 B 5 54 C 6 55 D 7 56 Name: 2013-01-02 00:00:00, dtype: int64 57 """ 58 59 print(df.loc[:,['A','B']]) 60 """ 61 A B 62 2013-01-01 0 1 63 2013-01-02 4 5 64 2013-01-03 8 9 65 2013-01-04 12 13 66 2013-01-05 16 17 67 2013-01-06 20 21 68 """ 69 70 print(df.loc['20130102',['A','B']]) 71 """ 72 A 4 73 B 5 74 Name: 2013-01-02 00:00:00, dtype: int64 75 """ 76 77 #採用位置進行選擇 iloc 78 print(df.iloc[3,1]) 79 # 13 80 81 print(df.iloc[3:5,1:3]) 82 """ 83 B C 84 2013-01-04 13 14 85 2013-01-05 17 18 86 """ 87 88 print(df.iloc[[1,3,5],1:3]) 89 """ 90 B C 91 2013-01-02 5 6 92 2013-01-04 13 14 93 2013-01-06 21 22 94 95 """ 96 97 #根據混合的這兩種 ix,採用混合選擇 ix, 其中選擇’A’和’C’的兩列,並選擇前三行的數據。 98 print(df.ix[:3,['A','C']]) 99 """ 100 A C 101 2013-01-01 0 2 102 2013-01-02 4 6 103 2013-01-03 8 10 104 """ 105 #經過判斷的篩選 106 print(df[df.A>8]) 107 """ 108 A B C D 109 2013-01-04 12 13 14 15 110 2013-01-05 16 17 18 19 111 2013-01-06 20 21 22 23 112 """
③、Pandas 設置值
1 #建立數據 2 import pandas as pd 3 import numpy as np 4 datas = pd.date_range("20190508", periods=6) 5 df = pd.DataFrame(np.arange(24).reshape((6, 4)), index=datas, columns=['A', 'B', 'C', 'D']) 6 print(df) 7 8 #利用索引或者標籤肯定須要修改值的位置: 9 df.iloc[2, 2] = 1111 10 df.loc['20190508', 'B'] = 2222 11 print(df) 12 13 #根據條件設置 14 df.B[df.A>4] = 0 15 """ 16 A B C D 17 2013-01-01 0 2222 2 3 18 2013-01-02 4 5 6 7 19 2013-01-03 8 0 1111 11 20 2013-01-04 12 0 14 15 21 2013-01-05 16 0 18 19 22 2013-01-06 20 0 22 23 23 """ 24 25 #按行或列設置,對整列作批處理, 加上一列 ‘F’, 並將 F 列全改成 NaN, 以下: 26 df['F'] = np.nan 27 """ 28 A B C D F 29 2013-01-01 0 2222 2 3 NaN 30 2013-01-02 4 5 6 7 NaN 31 2013-01-03 8 0 1111 11 NaN 32 2013-01-04 12 0 14 15 NaN 33 2013-01-05 16 0 18 19 NaN 34 2013-01-06 20 0 22 23 NaN 35 """ 36 37 #添加數據,用上面的方法也能夠加上 Series 序列(可是長度必須對齊)。 38 df['E'] = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130101',periods=6)) 39 """ 40 A B C D F E 41 2013-01-01 0 2222 2 3 NaN 1 42 2013-01-02 4 5 6 7 NaN 2 43 2013-01-03 8 0 1111 11 NaN 3 44 2013-01-04 12 0 14 15 NaN 4 45 2013-01-05 16 0 18 19 NaN 5 46 2013-01-06 20 0 22 23 NaN 6 47 """
④、Pandas 處理丟失數據
1 #如何刪除或者是填補這些 NaN 數據 2 #創建了一個6X4的矩陣數據而且把兩個位置置爲空 3 dates = pd.date_range('20130101', periods=6) 4 df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D']) 5 df.iloc[0,1] = np.nan 6 df.iloc[1,2] = np.nan 7 """ 8 A B C D 9 2013-01-01 0 NaN 2.0 3 10 2013-01-02 4 5.0 NaN 7 11 2013-01-03 8 9.0 10.0 11 12 2013-01-04 12 13.0 14.0 15 13 2013-01-05 16 17.0 18.0 19 14 2013-01-06 20 21.0 22.0 23 15 """ 16 #直接去掉有 NaN 的行或列, 可使用 dropna 17 df=df.dropna( 18 axis=0, # 0: 對行進行操做; 1: 對列進行操做 19 how='any' # 'any': 只要存在 NaN 就 drop 掉; 'all': 必須所有是 NaN 才 drop 20 ) 21 """ 22 A B C D 23 2013-01-03 8 9.0 10.0 11 24 2013-01-04 12 13.0 14.0 15 25 2013-01-05 16 17.0 18.0 19 26 2013-01-06 20 21.0 22.0 23 27 """ 28 29 #將 NaN 的值用其餘值代替, 好比代替成 0: 30 df.fillna(value=0) 31 """ 32 A B C D 33 2013-01-01 0 0.0 2.0 3 34 2013-01-02 4 5.0 0.0 7 35 2013-01-03 8 9.0 10.0 11 36 2013-01-04 12 13.0 14.0 15 37 2013-01-05 16 17.0 18.0 19 38 2013-01-06 20 21.0 22.0 23 39 """ 40 41 #判斷是否有缺失數據 NaN, 爲 True 表示缺失數據: 42 df.isnull() 43 """ 44 A B C D 45 2013-01-01 False True False False 46 2013-01-02 False False True False 47 2013-01-03 False False False False 48 2013-01-04 False False False False 49 2013-01-05 False False False False 50 2013-01-06 False False False False 51 """ 52 53 #檢測在數據中是否存在 NaN, 若是存在就返回 True: 54 np.any(df.isnull()) == True 55 # True
⑤、Pandas 導入導出
pandas
能夠讀取與存取的資料格式有不少種,像csv
、excel
、json
、html
與pickle
等
1 #讀取csv 2 import pandas as pd #加載模塊 3 4 #讀取csv 5 data = pd.read_csv('student.csv') 6 7 #打印出data 8 print(data) 9 10 #將資料存取成pickle 11 data.to_pickle('student.pickle')
⑥、Pandas 合併 concat
pandas
處理多組數據的時候每每會要用到數據的合併處理,使用 concat
是一種基本的合併方式.並且concat
中有不少參數能夠調整,合併成你想要的數據形式.
1 #axis (合併方向) 2 #axis=0是預設值,所以未設定任何參數時,函數默認axis=0 3 import pandas as pd 4 import numpy as np 5 6 #定義資料集 7 df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d']) 8 df2 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d']) 9 df3 = pd.DataFrame(np.ones((3,4))*2, columns=['a','b','c','d']) 10 11 #concat縱向合併 12 res = pd.concat([df1, df2, df3], axis=0) 13 14 #打印結果 15 print(res) 16 # a b c d 17 # 0 0.0 0.0 0.0 0.0 18 # 1 0.0 0.0 0.0 0.0 19 # 2 0.0 0.0 0.0 0.0 20 # 0 1.0 1.0 1.0 1.0 21 # 1 1.0 1.0 1.0 1.0 22 # 2 1.0 1.0 1.0 1.0 23 # 0 2.0 2.0 2.0 2.0 24 # 1 2.0 2.0 2.0 2.0 25 # 2 2.0 2.0 2.0 2.0 26 27 #ignore_index (重置 index) 28 #承上一個例子,並將index_ignore設定爲True 29 res = pd.concat([df1, df2, df3], axis=0, ignore_index=True) 30 31 #打印結果 32 print(res) 33 # a b c d 34 # 0 0.0 0.0 0.0 0.0 35 # 1 0.0 0.0 0.0 0.0 36 # 2 0.0 0.0 0.0 0.0 37 # 3 1.0 1.0 1.0 1.0 38 # 4 1.0 1.0 1.0 1.0 39 # 5 1.0 1.0 1.0 1.0 40 # 6 2.0 2.0 2.0 2.0 41 # 7 2.0 2.0 2.0 2.0 42 # 8 2.0 2.0 2.0 2.0 43 44 #join (合併方式) 45 #join='outer'爲預設值,所以未設定任何參數時,函數默認join='outer'。此方式是依照column來作縱向合併,有相同的column上下合併在一塊兒,其餘獨自的column個自成列,本來沒有值的位置皆以NaN填充。 46 import pandas as pd 47 import numpy as np 48 49 #定義資料集 50 df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3]) 51 df2 = pd.DataFrame(np.ones((3,4))*1, columns=['b','c','d','e'], index=[2,3,4]) 52 53 #縱向"外"合併df1與df2 54 res = pd.concat([df1, df2], axis=0, join='outer') 55 56 print(res) 57 # a b c d e 58 # 1 0.0 0.0 0.0 0.0 NaN 59 # 2 0.0 0.0 0.0 0.0 NaN 60 # 3 0.0 0.0 0.0 0.0 NaN 61 # 2 NaN 1.0 1.0 1.0 1.0 62 # 3 NaN 1.0 1.0 1.0 1.0 63 # 4 NaN 1.0 1.0 1.0 1.0 64 65 #join='inner',相同的column合併在一塊兒,其餘的會被拋棄。 66 #承上一個例子 67 68 #縱向"內"合併df1與df2 69 res = pd.concat([df1, df2], axis=0, join='inner') 70 71 #打印結果 72 print(res) 73 # b c d 74 # 1 0.0 0.0 0.0 75 # 2 0.0 0.0 0.0 76 # 3 0.0 0.0 0.0 77 # 2 1.0 1.0 1.0 78 # 3 1.0 1.0 1.0 79 # 4 1.0 1.0 1.0 80 81 #重置index並打印結果 82 res = pd.concat([df1, df2], axis=0, join='inner', ignore_index=True) 83 print(res) 84 # b c d 85 # 0 0.0 0.0 0.0 86 # 1 0.0 0.0 0.0 87 # 2 0.0 0.0 0.0 88 # 3 1.0 1.0 1.0 89 # 4 1.0 1.0 1.0 90 # 5 1.0 1.0 1.0 91 92 #join_axes (依照 axes 合併) 93 import pandas as pd 94 import numpy as np 95 96 #定義資料集 97 df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3]) 98 df2 = pd.DataFrame(np.ones((3,4))*1, columns=['b','c','d','e'], index=[2,3,4]) 99 100 #依照`df1.index`進行橫向合併 101 res = pd.concat([df1, df2], axis=1, join_axes=[df1.index]) 102 103 #打印結果 104 print(res) 105 # a b c d b c d e 106 # 1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN 107 # 2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 108 # 3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 109 110 #移除join_axes,並打印結果 111 res = pd.concat([df1, df2], axis=1) 112 print(res) 113 # a b c d b c d e 114 # 1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN 115 # 2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 116 # 3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 117 # 4 NaN NaN NaN NaN 1.0 1.0 1.0 1.0 118 119 #append (添加數據) 120 #append只有縱向合併,沒有橫向合併 121 import pandas as pd 122 import numpy as np 123 124 #定義資料集 125 df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d']) 126 df2 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d']) 127 df3 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d']) 128 s1 = pd.Series([1,2,3,4], index=['a','b','c','d']) 129 130 #將df2合併到df1的下面,以及重置index,並打印出結果 131 res = df1.append(df2, ignore_index=True) 132 print(res) 133 # a b c d 134 # 0 0.0 0.0 0.0 0.0 135 # 1 0.0 0.0 0.0 0.0 136 # 2 0.0 0.0 0.0 0.0 137 # 3 1.0 1.0 1.0 1.0 138 # 4 1.0 1.0 1.0 1.0 139 # 5 1.0 1.0 1.0 1.0 140 141 #合併多個df,將df2與df3合併至df1的下面,以及重置index,並打印出結果 142 res = df1.append([df2, df3], ignore_index=True) 143 print(res) 144 # a b c d 145 # 0 0.0 0.0 0.0 0.0 146 # 1 0.0 0.0 0.0 0.0 147 # 2 0.0 0.0 0.0 0.0 148 # 3 1.0 1.0 1.0 1.0 149 # 4 1.0 1.0 1.0 1.0 150 # 5 1.0 1.0 1.0 1.0 151 # 6 1.0 1.0 1.0 1.0 152 # 7 1.0 1.0 1.0 1.0 153 # 8 1.0 1.0 1.0 1.0 154 155 #合併series,將s1合併至df1,以及重置index,並打印出結果 156 res = df1.append(s1, ignore_index=True) 157 print(res) 158 # a b c d 159 # 0 0.0 0.0 0.0 0.0 160 # 1 0.0 0.0 0.0 0.0 161 # 2 0.0 0.0 0.0 0.0 162 # 3 1.0 2.0 3.0 4.0
⑦、Pandas 合併 merge
pandas中的merge和concat相似,但主要是用於兩組有key column的數據,統一索引的數據. 一般也被用在Database的處理當中。
1 #依據一組key合併 2 import pandas as pd 3 4 #定義資料集並打印出 5 left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'], 6 'A': ['A0', 'A1', 'A2', 'A3'], 7 'B': ['B0', 'B1', 'B2', 'B3']}) 8 right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'], 9 'C': ['C0', 'C1', 'C2', 'C3'], 10 'D': ['D0', 'D1', 'D2', 'D3']}) 11 12 print(left) 13 # A B key 14 # 0 A0 B0 K0 15 # 1 A1 B1 K1 16 # 2 A2 B2 K2 17 # 3 A3 B3 K3 18 19 print(right) 20 # C D key 21 # 0 C0 D0 K0 22 # 1 C1 D1 K1 23 # 2 C2 D2 K2 24 # 3 C3 D3 K3 25 26 #依據key column合併,並打印出 27 res = pd.merge(left, right, on='key') 28 29 print(res) 30 A B key C D 31 # 0 A0 B0 K0 C0 D0 32 # 1 A1 B1 K1 C1 D1 33 # 2 A2 B2 K2 C2 D2 34 # 3 A3 B3 K3 C3 D3 35 36 #依據兩組key合併 37 #合併時有4種方法how = ['left', 'right', 'outer', 'inner'],預設值how='inner'。 38 import pandas as pd 39 40 #定義資料集並打印出 41 left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'], 42 'key2': ['K0', 'K1', 'K0', 'K1'], 43 'A': ['A0', 'A1', 'A2', 'A3'], 44 'B': ['B0', 'B1', 'B2', 'B3']}) 45 right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'], 46 'key2': ['K0', 'K0', 'K0', 'K0'], 47 'C': ['C0', 'C1', 'C2', 'C3'], 48 'D': ['D0', 'D1', 'D2', 'D3']}) 49 50 print(left) 51 # A B key1 key2 52 # 0 A0 B0 K0 K0 53 # 1 A1 B1 K0 K1 54 # 2 A2 B2 K1 K0 55 # 3 A3 B3 K2 K1 56 57 print(right) 58 # C D key1 key2 59 # 0 C0 D0 K0 K0 60 # 1 C1 D1 K1 K0 61 # 2 C2 D2 K1 K0 62 # 3 C3 D3 K2 K0 63 64 #依據key1與key2 columns進行合併,並打印出四種結果['left', 'right', 'outer', 'inner'] 65 res = pd.merge(left, right, on=['key1', 'key2'], how='inner') 66 print(res) 67 # A B key1 key2 C D 68 # 0 A0 B0 K0 K0 C0 D0 69 # 1 A2 B2 K1 K0 C1 D1 70 # 2 A2 B2 K1 K0 C2 D2 71 72 res = pd.merge(left, right, on=['key1', 'key2'], how='outer') 73 print(res) 74 # A B key1 key2 C D 75 # 0 A0 B0 K0 K0 C0 D0 76 # 1 A1 B1 K0 K1 NaN NaN 77 # 2 A2 B2 K1 K0 C1 D1 78 # 3 A2 B2 K1 K0 C2 D2 79 # 4 A3 B3 K2 K1 NaN NaN 80 # 5 NaN NaN K2 K0 C3 D3 81 82 res = pd.merge(left, right, on=['key1', 'key2'], how='left') 83 print(res) 84 # A B key1 key2 C D 85 # 0 A0 B0 K0 K0 C0 D0 86 # 1 A1 B1 K0 K1 NaN NaN 87 # 2 A2 B2 K1 K0 C1 D1 88 # 3 A2 B2 K1 K0 C2 D2 89 # 4 A3 B3 K2 K1 NaN NaN 90 91 res = pd.merge(left, right, on=['key1', 'key2'], how='right') 92 print(res) 93 # A B key1 key2 C D 94 # 0 A0 B0 K0 K0 C0 D0 95 # 1 A2 B2 K1 K0 C1 D1 96 # 2 A2 B2 K1 K0 C2 D2 97 # 3 NaN NaN K2 K0 C3 D3 98 99 #Indicator ,indicator=True會將合併的記錄放在新的一列。 100 import pandas as pd 101 102 #定義資料集並打印出 103 df1 = pd.DataFrame({'col1':[0,1], 'col_left':['a','b']}) 104 df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]}) 105 106 print(df1) 107 # col1 col_left 108 # 0 0 a 109 # 1 1 b 110 111 print(df2) 112 # col1 col_right 113 # 0 1 2 114 # 1 2 2 115 # 2 2 2 116 117 # 依據col1進行合併,並啓用indicator=True,最後打印出 118 res = pd.merge(df1, df2, on='col1', how='outer', indicator=True) 119 print(res) 120 # col1 col_left col_right _merge 121 # 0 0.0 a NaN left_only 122 # 1 1.0 b 2.0 both 123 # 2 2.0 NaN 2.0 right_only 124 # 3 2.0 NaN 2.0 right_only 125 126 # 自定indicator column的名稱,並打印出 127 res = pd.merge(df1, df2, on='col1', how='outer', indicator='indicator_column') 128 print(res) 129 # col1 col_left col_right indicator_column 130 # 0 0.0 a NaN left_only 131 # 1 1.0 b 2.0 both 132 # 2 2.0 NaN 2.0 right_only 133 # 3 2.0 NaN 2.0 right_only 134 135 #依據index合併 136 import pandas as pd 137 138 #定義資料集並打印出 139 left = pd.DataFrame({'A': ['A0', 'A1', 'A2'], 140 'B': ['B0', 'B1', 'B2']}, 141 index=['K0', 'K1', 'K2']) 142 right = pd.DataFrame({'C': ['C0', 'C2', 'C3'], 143 'D': ['D0', 'D2', 'D3']}, 144 index=['K0', 'K2', 'K3']) 145 146 print(left) 147 # A B 148 # K0 A0 B0 149 # K1 A1 B1 150 # K2 A2 B2 151 152 print(right) 153 # C D 154 # K0 C0 D0 155 # K2 C2 D2 156 # K3 C3 D3 157 158 #依據左右資料集的index進行合併,how='outer',並打印出 159 res = pd.merge(left, right, left_index=True, right_index=True, how='outer') 160 print(res) 161 # A B C D 162 # K0 A0 B0 C0 D0 163 # K1 A1 B1 NaN NaN 164 # K2 A2 B2 C2 D2 165 # K3 NaN NaN C3 D3 166 167 #依據左右資料集的index進行合併,how='inner',並打印出 168 res = pd.merge(left, right, left_index=True, right_index=True, how='inner') 169 print(res) 170 # A B C D 171 # K0 A0 B0 C0 D0 172 # K2 A2 B2 C2 D2 173 174 #解決overlapping的問題 175 import pandas as pd 176 177 #定義資料集 178 boys = pd.DataFrame({'k': ['K0', 'K1', 'K2'], 'age': [1, 2, 3]}) 179 girls = pd.DataFrame({'k': ['K0', 'K0', 'K3'], 'age': [4, 5, 6]}) 180 181 #使用suffixes解決overlapping的問題 182 res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='inner') 183 print(res) 184 # age_boy k age_girl 185 # 0 1 K0 4 186 # 1 1 K0 5
⑧、Pandas plot 出圖
1 #建立一個Series 2 #隨機生成1000個數據,Series 默認的 index 就是從0開始的整數,可是這裏我顯式賦值以便讓你們看的更清楚 3 # 隨機生成1000個數據 4 data = pd.Series(np.random.randn(1000),index=np.arange(1000)) 5 6 # 爲了方便觀看效果, 咱們累加這個數據 7 data.cumsum() 8 9 # pandas 數據能夠直接觀看其可視化形式 10 data.plot() 11 12 plt.show() 13 14 #Dataframe 可視化 15 #生成一個1000*4 的DataFrame,並對他們累加 16 data = pd.DataFrame( 17 np.random.randn(1000,4), 18 index=np.arange(1000), 19 columns=list("ABCD") 20 ) 21 data.cumsum() 22 data.plot() 23 plt.show()
常常會用到還有scatter
,這個會顯示散點圖,首先給你們說一下在 pandas 中有多少種方法bar,hist,box,kde,area,scatter,hexbin
1 #scatter只有x,y兩個屬性 2 ax = data.plot.scatter(x='A',y='B',color='DarkBlue',label='Class1') 3 #再畫一個在同一個ax上面,選擇不同的數據列,不一樣的 color 和 label 4 #將之下這個 data 畫在上一個 ax 上面 5 data.plot.scatter(x='A',y='C',color='LightGreen',label='Class2',ax=ax) 6 plt.show()