Python Data Analysis Library 或 pandas 是基於NumPy 的一種工具,該工具是爲了解決數據分析任務而建立的。Pandas 歸入了大量庫和一些標準的數據模型,提供了高效地操做大型數據集所需的工具。pandas提供了大量能使咱們快速便捷地處理數據的函數和方法。你很快就會發現,它是使Python成爲強大而高效的數據分析環境的重要因素之一。css
Series是一種相似與一維數組的對象,由下面兩個部分組成:html
from pandas import Series,DataFrame import numpy as np
兩種建立方式:java
(1) 由列表或numpy數組建立python
默認索引爲0到N-1的整數型索引 #使用列表建立Series(列表也能夠換爲numpy的array) Series([1,2,3,4,5],index=['a','b','c','d','e'],name='Hello') # a 1 b 2 c 3 d 4 e 5 Name: Hello, dtype: int64
(2) 由字典建立:不能在使用index.可是依然存在默認索引數組
dict = { 'hello':12, 'hey':30 } Series(data=dict) # hello 12 hey 30 dtype: int64
可使用中括號取單個索引(此時返回的是元素類型),或者中括號裏一個列表取多個索引(此時返回的是一個Series類型)。markdown
(1) 顯式索引:數據結構
- 使用index中的元素做爲索引值 - 使用s.loc[](推薦):注意,loc中括號中放置的必定是顯示索引
注意,此時是閉區間app
s1 = Series(data=[1,2,3],index=['a','b','c']) s1 # a 1 b 2 c 3 dtype: int64 s1.loc["a"] # 1
(2) 隱式索引:dom
- 使用整數做爲索引值 - 使用.iloc[](推薦):iloc中的中括號中必須放置隱式索引
注意,此時是半開區間函數
s1.loc["a":"c"] # a 1 b 2 c 3 dtype: int64
能夠把Series當作一個定長的有序字典
向Series增長一行:至關於給字典增長一組鍵值對
s1['a'] = 10 s1 # a 10 b 2 c 3 dtype: int64 s1['d']=20 s1 # a 10 b 2 c 3 d 20 dtype: int64
s1 = Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) s1 # a 1 b 2 c 3 d 4 dtype: int64 s1.values # array([1, 2, 3, 4], dtype=int64) s1.index # Index(['a', 'b', 'c', 'd'], dtype='object')
可使用pd.isnull(),pd.notnull(),或s.isnull(),notnull()函數檢測缺失數據
s1 = Series(data=[1,2,3,4,5],index=['a','b','c','d','e']) s2 = Series(data=[1,2,3,4,5],index=['a','g','c','e','f']) s3 = s1 + s2 s3 # a 2.0 b NaN c 6.0 d NaN e 9.0 f NaN g NaN dtype: float64 s3.isnull() # a False b True c False d True e False f True g True dtype: bool s3[~s3.isnull()] # 利用布爾索引 獲取不是NaN的數據 # a 2.0 c 6.0 e 9.0 dtype: float64 s3.notnull() # a True b False c True d False e True f False g False dtype: bool s3[s3.notnull()] # a 2.0 c 6.0 e 9.0 dtype: float64
(1) + - * /
(2) add() sub() mul() div() : s1.add(s2,fill_value=0)
(3) Series之間的運算
sr3 =sr1+sr2 sr3 # a 33.0 b NaN c 32.0 d 45.0 dtype: float64 sr3.dropna() # 獲取全部不是NaN的數 # a 33.0 c 32.0 d 45.0 dtype: float64 sr3.fillna(0) # 使用相近的數 自動填充NaN # a 33.0 b 0.0 c 32.0 d 45.0 dtype: float64
DataFrame是一個【表格型】的數據結構。DataFrame由按必定順序排列的多列數據組成。設計初衷是將Series的使用場景從一維拓展到多維。DataFrame既有行索引,也有列索引。
最經常使用的方法是傳遞一個字典來建立。DataFrame以字典的鍵做爲每一【列】的名稱,以字典的值(一個數組)做爲每一列。
此外,DataFrame會自動加上每一行的索引。
使用字典建立的DataFrame後,則columns參數將不可被使用。
同Series同樣,若傳入的列與字典的鍵不匹配,則相應的值爲NaN。
dict = { "java":[90,22,66], 'python':[12,33,66] } DataFrame(data=dict,index=['zhangsan','lisi','wangwu'])
DataFrame(data=np.random.randint(0,100,size=(3,6)))
DataFrame屬性:values、columns、index、shape
(1) 對列進行索引
- 經過相似字典的方式 df['q'] - 經過屬性的方式 df.q
能夠將DataFrame的列獲取爲一個Series。返回的Series擁有原DataFrame相同的索引,且name屬性也已經設置好了,就是相應的列名。
(2) 對行進行索引
- 使用.loc[]加index來進行行索引 - 使用.iloc[]加整數來進行行索引
一樣返回一個Series,index爲原來的columns。
df.loc['java'] # zhangsan 100 lisi 88 wangwu 78 Name: java, dtype: int64
【注意】 直接用中括號時:
1) DataFrame之間的運算
同Series同樣:
有兩種丟失數據:
None是Python自帶的,其類型爲python object。所以,None不能參與到任何計算中。
np.nan是浮點類型,能參與到計算中。但計算的結果老是NaN。
1) pandas中None與np.nan都視做np.nan
2) pandas處理空值操做
isnull()
notnull()
dropna()
: 過濾丟失數據fillna()
: 填充丟失數據#建立DataFrame,給其中某些元素賦值爲nan df = DataFrame(data=np.random.randint(0,100,size=(5,8)),index=['a','b','c','d','e'],columns=['A','B','C','D','E','F','G','H']) df['B']['c'] = None df['F']['d'] = np.nan df['D']['c'] = None df
(1)判斷函數
isnull()
notnull()
(2)過濾函數
3) 填充函數 Series/DataFrame
fillna()
:value和method參數最多見的方法是給DataFrame構造函數的index或者columns參數傳遞兩個或更多的數組
df = DataFrame(data=np.random.randint(80,100,size=(2,4)),index=['tom','jay'],columns=[['qz','qz','qm','qm'],['chinese','math','chinese','math']]) df
切片操做
所謂的聚合操做:平均數,方差,最大值,最小值……
pandas的拼接分爲兩種:
pandas使用pd.concat函數,與np.concatenate函數相似,只是多了一些參數:
objs
axis=0
keys:列表,列表元素表示的是進行級聯的df的一個名稱 join='outer' / 'inner':表示的是級聯的方式,outer會將全部的項進行級聯(忽略匹配和不匹配),而inner只會將匹配的項級聯到一塊兒,不匹配的不級聯 ignore_index=False
匹配級聯
不匹配級聯
不匹配指的是級聯的維度的索引不一致。例如縱向級聯時列索引不一致,橫向級聯時行索引不一致
有2種鏈接方式: