pandas 我們課程后續用得最多的一個模塊,主要用於進行數據探索和數據分析
Pandas 是基於 NumPy
pandas庫與numpy的區別
• Numpy
• 基礎數據類型
• 關注數據的結構表達
• 維度:數據間關系
-------------------------------------------------------------
• Pandas
• 擴展數據類型
• 關注數據的應用表達
• 數據與索引間的關系
Pandas 的數據結構
Series 是一種一維數組,和 NumPy 里的數組很相 似。事實上,Series 基本上就是基於 NumPy 的數組對象來的。和 NumPy 的數組不同,Series 能為數據自定義標簽,也就是索引(index),然后通過索引來訪問數組中的數據。
通常,我們導入如下:
In [1]: import numpy as np
In [2]: import pandas as pd
對象創建
Series類型由一組數據及與之相關的數據索引組成。
Serjes可以由如下類型創建:
1.python列表,index與列表元素個數一致
2.標量值,index表達Series類型的尺寸
3.ndarray,索引和數據都可以通過ndarray創建
4.python 字典 鍵值中的鍵是索引,index從字典中進行選擇操作
5.其他函數,range()函數等。
創建一個Series
通過傳遞值的列表,讓大熊貓創建一個默認的整數索引:
s = pd.Series(date,index)
上面的 data 參數可以是任意數據對象,比如字典、列表甚至是 NumPy 數組,而index 參數則是對 data 的索引值,類似字典的 key。
下面這個例子里,將創建一個 Series 對象,並用字符串對數字列表進行索引:
In [22]: countries = ['USA','Nigeria','France','Ghana']
...: my_date = [100,200,300,400]
...: s = pd.Series(my_date,tcountries)
In [23]: s
Out[23]:
USA 100
Nigeria 200
France 300
Ghana 400
dtype: int64 #Numpy 中的數據類型
注意:請記住, index 參數是可省略的,你可以選擇不輸入這個參數。如果不帶 index 參數,Pandas 會自動用默認 index 進行索引,類似數組,索引值是 [0, ..., len(data) - 1] ,如下所示:
從 NumPy 數組對象創建 Series:
In [5]: np_arr = np.array(my_date)
In [6]: pd.Series(np_arr)
Out[6]:
0 100
1 200
2 300
3 400
dtype: int32
#從字典創建Series數組
In [7]: my_dict = {'a':50,'b':60,'c':70,'d':80}
...: pd.Series(my_dict)
Out[7]:
a 50
b 60
c 70
d 80
dtype: int64
#如果傳入index 從字典的鍵進行挑選,無值則為NaN,按index的順序來
In [11]: e =pd.Series(my_dict,index=['c','a','b','d','f'])
In [12]: e
Out[12]:
c 70.0
a 50.0
b 60.0
d 80.0
f NaN
dtype: float64
Series類型的基本操作 |
---|
Series類型包括index和values兩部分。 |
Series類型的操作類似ndarray類型。 |
Series類型的操作類似python字典類型。 |
Series對象可以根據 .index 和.values 獲得Series對象的索引和數據,示例代碼如下: |
In [15]: import pandas as pd
# 導入pandas as pd
In [16]: b= pd.Series([9,8,7,6],['a','b','c','d'])
#生成一個Series 對象
In [17]: b
Out[17]:
a 9
b 8
c 7
d 6
dtype: int64
In [18]: b.index #獲取所有的索引
Out[18]: Index(['a', 'b', 'c', 'd'], dtype='object')
In [19]: b.values #獲取所有的values值
Out[19]: array([9, 8, 7, 6], dtype=int64)
In [20]: b['b']
Out[20]: 8
In [21]: b[1]
Out[21]: 8
#自動索引和自定義索引並存
In [22]: b[['c','d',0]]
Out[22]:
c 7.0
d 6.0
0 NaN
dtype: float64
In [23]: b[['c','d','a']]
Out[23]:
c 7
d 6
a 9
dtype: int64
#兩套索引共存但是不能混合使用
Series類型的操作類似ndarray類型。 |
---|
索引方法相同,都采用[] |
NumPy中運算和操作可用於Series類型 |
可以通過自定義索引的列表進行切片。 |
可以通過自定義索引進行切片,如果存在自定義索引,則一同被切片 |
In [24]: b[3] #通過自動索引3 取到5
Out[24]: 6
In [25]: b[:3]# 切片0-3之內的值並且還有其索引
Out[25]:
a 9
b 8
c 7
dtype: int64
In [26]: b[b>b.median()] #大於中位數的
Out[26]:
a 9
b 8
dtype: int64
In [27]: np.exp(b) #e的x方運算
Out[27]:
a 8103.083928
b 2980.957987
c 1096.633158
d 403.428793
dtype: float64
Series類型的操作類似python字典類型。 |
---|
通過自定義索引訪問。 |
保留字in操作 |
使用.get()方法 |
In [28]: 'c' in b
Out[28]: True #判斷自定義索引是否在b中
In [29]: 0 in b
Out[29]: False #無法判斷自動索引 ok?
In [30]: b.get('f',100) #b中提取f索引的值100,如果不存在則返回100,存在返回f對應的值如果不定義100則返回空
Out[30]: 100
Series的對齊問題,示例代碼如下:
In [34]: a =pd.Series([1,2,3],['c','d','e'])
#生成一個Series對象a
In [35]: a+b
Out[35]: #結果是一個並集只有同索引的值可以相加,其他值默認都是NaN
a NaN
b NaN
c 8.0
d 8.0
e NaN
dtype: float64
PS:
Series類型在運算中會自動對齊不同索引的數據
Series類型的name屬性:
Series對象和索引都可以有一個名字,存儲在.name中。
In [37]: b.name In [38]: b.name='Series 對象' #給b.name 賦值 In [39]: b.index.name='索引列' #給index.name賦值 In [40]: b Out[40]: 索引列 a 9 b 8 c 7 d 6 Name: Series 對象, dtype: int64
Series類型的修改
Series對象可以隨時修改並即刻生效。
In [42]: b Out[42]: 索引列 a 9 b 8 c 7 d 6 Name: Series 對象, dtype: int64 In [43]: b['a'] Out[43]: 9 In [44]: b['a']=15 In [45]: b.name = 'Series' #賦值生效 In [46]: b Out[46]: 索引列 a 15 b 8 c 7 d 6 Name: Series, dtype: int64 In [47]: b.name = 'New Series' In [48]: b['b','c'] = 20 In [49]: b Out[49]: 索引列 a 15 b 20 c 20 d 6 Name: New Series, dtype: int64