首先pandas的作者就是這本書的作者
對於Numpy,我們處理的對象是矩陣
pandas是基於numpy進行封裝的,pandas的處理對象是二維表(tabular, spreadsheet-like),和矩陣的區別就是,二維表是有元數據的
用這些元數據作為index更方便,而Numpy只有整形的index,但本質是一樣的,所以大部分操作是共通的
大家碰到最多的二維表應用,關系型數據庫中的表,有列名和行號,這些就是元數據
當然你可以用抽象的矩陣來對這些二維表做統計,但使用pandas會更方便
Introduction to pandas Data Structures
Series
A Series is a one-dimensional array-like object containing an array of data (of any NumPy data type) and an associated array of data labels, called its index.
簡單的理解,就是字典,或一維表;不顯式指定index時,會自動添加 0 through N - 1的整數作為index
這里可以簡單的替換index,生成新的series,
大家想想,對於Numpy而言,沒有顯式的指定index,但也是可以通過整形的index取到數據的,這里的index其實本質上和numpy的整形index是一樣的
所以對於Numpy的操作,也同樣適用於pandas
同時,上面說了series其實就是字典,所以也可以用python字典來初始化
DataFrame
A DataFrame represents a tabular, spreadsheet-like data structure containing an ordered collection of columns, each of which can be a different value type (numeric, string, boolean, etc.).
如果接觸過R,應該對DataFrame很熟悉,其實pandas就從某種程度上模擬出R的一些功能
所以如果用python也可以像R一樣方便的做統計,那何必要再去用R
上面Series是字典或一維表,
DataFrame是二維表,也可以看作是series的字典
指定了列名,行名是自動生成的
同時也可以指定行名,這里增加了debt列,但是沒有數據,所以是NaN
可以為debt,賦值
取行,用ix
也可以用嵌套字典來創建Dataframe,其實是series的字典,series本身就是字典,所以就是嵌套的字典
可以像numpy矩陣一樣,轉置
Essential Functionality
下面看看到底pandas在這些數據結構上提供了哪些方便的functions
Reindexing
A critical method on pandas objects is reindex, which means to create a new object with the data conformed to a new index.
其實就是更改indexing
增加e,並默認填上0
還可以通過method參數,來指定填充方式
可以選擇向前或向后填充
對於二維表,可以在index和columns上同時進行reindex
reindex的參數,
Dropping entries from an axis
用axis指定維度,對於二維表,行是0,列是1
Indexing, selection, and filtering
基本和Numpy差不多
Arithmetic and data alignment
數據對齊和自動填充是pandas比較方便的一點
In [136]: df1 = DataFrame(np.arange(12.).reshape((3, 4)), columns=list('abcd'))In [137]: df2 = DataFrame(np.arange(20.).reshape((4, 5)), columns=list('abcde'))
可以看到默認情況下,只有兩個df都有的情況下,才會相加,否則為NaN
我覺得大部分情況,應該是希望有一個就加一個,即把沒有的初始化為0
除了add,還支持
Function application and mapping
1. Element-wise:NumPy ufuncs (element-wise array methods) work fine with pandas objects:
另一種element-wise,使用applymap
2. 可以將func apply到每一行或每一列
比較復雜的case
3.對於某個行或列,即series進行map
Summarizing and Computing Descriptive Statistics
提供很多類似R的統計函數,
提供類似R中的descirbe,很方便
對非數值型,執行describe
匯總表,
Correlation and Covariance,相關系數和協方差
對MSFT和IBM之間求相關系數和協方差
也可以求出相關系數矩陣和協方差矩陣
Unique Values, Value Counts, and Membership
In [217]: obj = Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])
In [218]: uniques = obj.unique()
In [219]: uniques
Out[219]: array([c, a, d, b], dtype=object)
In [220]: obj.value_counts()
Out[220]:
c 3
a 3
b 2
d 1
Handling Missing Data
提供一些用於處理missing data的工具函數
其中fillna復雜些,
Hierarchical Indexing
Hierarchical indexing is an important feature of pandas enabling you to have multiple (two or more) index levels on an axis. Somewhat abstractly, it provides a way for you to work with higher dimensional data in a lower dimensional form.
可以使用多層分級的index,其實本質等同於增加一維,所以相當於用低維來模擬高維數據
並且是支持,通過unstack和stack來還原多維數據的
Pandas還提供其他功能,尤其是ETL功能,方便數據處理
比如和各種文件讀入和寫出的功能
cleaning, transform(基於map), merge(join)……