<code>from pandas import Series, DataFrame import pandas as pd /<code>
要使用pandas,首先就得熟悉它的兩個主要數據結構:Series和DateFrame。雖然它們並不能解決所有問題,但它們為大多數應用提供了一種可靠的、易於使用的基礎。
Series是一種類似於一位數組的對象,它由一組數據(各種NumPy數據類型)以及一組與之相關的數據標籤(即索引)組成。僅有一組數據即可產生最簡單的Series:
In [26]:
<code>obj = Series([4,7,5,-3]) obj /<code>
Out[26]:
<code>0 4 1 7 2 5 3 -3 dtype: int64/<code>
Series的字符表現形式為:索引在左邊,值在右邊。由於我們沒有為數據指定索引,於是會自動創建一個0到N-1(N為數據的長度)的整數型索引。你可以通過Series的values和index屬性獲取其數組表示形式的索引對象:
In [5]:
<code>obj.index /<code>
Out[5]:
<code>RangeIndex(start=0, stop=4, step=1)/<code>
In [6]:
<code>obj.values /<code>
Out[6]:
<code>array([ 4, 7, 5, -3])/<code>
通常,我們希望所創建的Series帶有一個可以對各個數據點進行標記的索引:
In [2]:
<code>obj2 = Series([4,7,-5,3], index=['d','b','a','c']) obj2 /<code>
Out[2]:
<code>d 4 b 7 a -5 c 3 dtype: int64/<code>
In [3]:
<code>obj2.index /<code>
Out[3]:
<code>Index(['d', 'b', 'a', 'c'], dtype='object')/<code>
與普通的NumPy數據相比,你可以通過索引的方式選取Series中的單個或一組值:
In [4]:
<code>obj2['a'] /<code>
Out[4]:
<code>-5/<code>
In [5]:
<code>obj2['d'] = 6 obj2 /<code>
Out[5]:
<code>d 6 b 7 a -5 c 3 dtype: int64/<code>
In [6]:
<code>obj2[['a','b','c','d']] /<code>
Out[6]:
<code>a -5 b 7 c 3 d 6 dtype: int64/<code>
NumPy數組運算(如根據布爾型數組進行過濾、標量乘法、應用數學函數等)都會保留索引和值之間的鏈接
In [7]:
<code>obj2 /<code>
Out[7]:
<code>d 6 b 7 a -5 c 3 dtype: int64/<code>
In [8]:
<code>obj2[obj2 > 0] /<code>
Out[8]:
<code>d 6 b 7 c 3 dtype: int64/<code>
In [9]:
<code>obj2 * 2 /<code>
Out[9]:
<code>d 12 b 14 a -10 c 6 dtype: int64/<code>
In [11]:
<code>import numpy as np np.exp(obj2) /<code>
Out[11]:
<code>d 403.428793 b 1096.633158 a 0.006738 c 20.085537 dtype: float64/<code>
還可以將Series看成是一個定長的有序字典,因為它是索引值到數據值的一個映射。它可以用在許多原本需要字典參數的函數中:
In [12]:
<code>'b' in obj2 /<code>
Out[12]:
<code>True/<code>
In [13]:
<code>'e' in obj2 /<code>
Out[13]:
<code>False/<code>
如果數據被存放在一個Python字典中,也可以直接通過這個字典來創建Series:
In [14]:
<code>sdata = {'Ohio':35000,'Texas':71000,'Oregon':16000,'Utah':5000} /<code>
In [15]:
<code>obj3 = Series(sdata) obj3 /<code>
Out[15]:
<code>Ohio 35000 Texas 71000 Oregon 16000 Utah 5000 dtype: int64/<code>
如果只傳入一個字典,則結果Series中的索引就是原字典的鍵(有序排列)。
In [32]:
<code>states = ['California','Ohio','Oregon','Texas'] /<code>
In [33]:
<code>obj4 = Series(sdata, index=states) /<code>
In [34]:
<code>obj4 /<code>
Out[34]:
<code>California NaN Ohio 35000.0 Oregon 16000.0 Texas 71000.0 dtype: float64/<code>
在上面的例子中,sdata中跟states索引相匹配的那3個值會被找出來並放到相應的位置上,但由於'California'所對應的sdata值找不到,所以其結果就為NaN(即'非數字',在pandas中,它用於表示缺失或NA值)。我將使用缺失或NA表示缺失數據。pandas的isnull和notnull函數可用於檢測缺失數據:
In [35]:
<code>pd.isnull(obj4) /<code>
Out[35]:
<code>California True Ohio False Oregon False Texas False dtype: bool/<code>
In [36]:
<code>pd.notnull(obj4) /<code>
Out[36]:
<code>California False Ohio True Oregon True Texas True dtype: bool/<code>
Series也有類似的實例方法:
In [37]:
<code>obj4.isnull() /<code>
Out[37]:
<code>California True Ohio False Oregon False Texas False dtype: bool/<code>
在後面,我也會涉及到如何處理缺失數據的詳細內容。
對於許多應用而言,Series最重要的一個功能是:它在算術運算中會自動對齊不同索引的數據:
In [38]:
<code>obj3 + obj4 /<code>
Out[38]:
<code>California NaN Ohio 70000.0 Oregon 32000.0 Texas 142000.0 Utah NaN dtype: float64/<code>
數據對齊功能,後面會單獨介紹。
Series對象本身及其索引都有一個name屬性,該屬性跟pandas其他的關鍵功能關係非常密切:
In [39]:
<code>obj4.name = 'population' obj4.index.name = 'state' obj4 /<code>
Out[39]:
<code>state California NaN Ohio 35000.0 Oregon 16000.0 Texas 71000.0 Name: population, dtype: float64/<code>
Series的索引可以通過賦值的方式就地修改:
In [40]:
<code>obj.index = ['Bob','Steve','Jeff','Ryan'] /<code>
In [41]:
<code>obj /<code>
Out[41]:
<code>Bob 4 Steve 7 Jeff 5 Ryan -3 dtype: int64/<code>