pandas采用了很多Numpy的代碼風(fēng)格,但是最大的不同在于pandas用來處理表格型或者異質(zhì)類數(shù)據(jù)。而Numpy則相反,它更適合處理同質(zhì)型的數(shù)值類數(shù)組數(shù)據(jù)。
Series
Series是一種一維的數(shù)組型對象,它包含了一個(gè)值序列,并且包含了數(shù)據(jù)標(biāo)簽,也就是索引。
import pandas as pd
from pandas import Series, DataFrame
obj = pd.Series([4, 7, -5, 3])
print(obj)
#輸出:
#0 4
#1 7
#2 -5
#3 3
這里我們看到了左側(cè)0-4是索引,右邊是值。
可以通過values和index來獲取對應(yīng)的值和索引。
print(obj.values)
print(obj.index)
#[ 4 7 -5 3]
#RangeIndex(start=0, stop=4, step=1)
上面用的系統(tǒng)默認(rèn)的索引,其實(shí)可以自己創(chuàng)建一個(gè)索引標(biāo)簽。
obj2 = pd.Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
print(obj2)
# d 4
# b 7
# a -5
# c 3
# dtype: int64
print(obj2.index)
Index(['d', 'b', 'a', 'c'], dtype='object')
上面發(fā)現(xiàn),我們自己定義的標(biāo)簽替換了系統(tǒng)默認(rèn)的標(biāo)簽。后期我們也可以去修改index。
obj = pd.Series([4, 7, -5, 3])
print(obj)
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
print(obj)
輸出:
0 4
1 7
2 -5
3 3
dtype: int64
Bob 4
Steve 7
Jeff -5
Ryan 3
dtype: int64
這樣我們就可以用我們自己的標(biāo)簽來讀取數(shù)據(jù)或者修改數(shù)據(jù)。
obj2['a']
# -5
obj2['d'] = 6
obj2[['c', 'a', 'd']]
# c 3
# a -5
# d 6
# dtype: int64
上面的代碼中,我們用了標(biāo)簽列表來獲取數(shù)據(jù)。
可以用Numpy相同的風(fēng)格操作,比如布爾值過濾,標(biāo)量相乘,數(shù)學(xué)函數(shù)等。
print(obj2[obj2 > 0])
print(obj2 * 2)
print(np.exp(obj2))
輸出結(jié)果:
d 6
b 7
c 3
dtype: int64
d 12
b 14
a -10
c 6
dtype: int64
d 403.428793
b 1096.633158
a 0.006738
c 20.085537
dtype: float64
其實(shí)可以這樣理解Series,它是一個(gè)長度固定且有序的字典,因?yàn)槠涫菍⑺饕蹬c數(shù)據(jù)值按位置配對。所以我們可以使用字典中方法用于Series中。
'b' in obj2 # True
'e' in obj2 # False
如果已經(jīng)有數(shù)據(jù)剛好是Python中的字典,那么可以直接使用這個(gè)字典來生成Series。
sdata = {'Ohio':35000, 'Texas':71000, 'Oregon':16000, 'Utah':5000}
obj3 = pd.Series(sdata)
print(obj3)
輸出:
Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
dtype: int64
用字典轉(zhuǎn)換的時(shí)候,直接利用了字典的key和value配對,同時(shí)按照字典排號的字典鍵作為索引的順序。同樣,可以按照自己的要求來生成索引順序。
states = ['California', 'Ohio', 'Oregon', 'Texas']
obj4 = pd.Series(sdata, index = states)
輸出:
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
dtype: float64
上面的數(shù)據(jù),我們發(fā)現(xiàn)California的value為NaN,這是因?yàn)閷?yīng)的索引在sdata中找不到對應(yīng)的key,所以缺失的值用NaN代替。而states中沒有Utah,所以舍棄掉。
pandas中使用isnull和notnull函數(shù)來檢查缺失數(shù)據(jù)。
pd.isnull(obj4)
# 或 obj4.isnull()
pd.notnull(obj4)
# 或 obj4.notnull()
在pandas中,其數(shù)據(jù)操作中自動對齊索引是Series中非常有用的特性。
print(obj3)
print(obj4)
print(obj3 + obj4)
輸出:
Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
dtype: int64
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
dtype: float64
California NaN
Ohio 70000.0
Oregon 32000.0
Texas 142000.0
Utah NaN
dtype: float64
發(fā)現(xiàn)obj3+obj4結(jié)果類似于數(shù)據(jù)庫中的join操作。會把兩組數(shù)據(jù)合并,缺省的值會用NaN表示。
Series對象自身和其索引都有一個(gè)name熟悉。
obj4.name = 'population'
obj4.index.name = 'state'
print(obj4)
輸出:
state
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
Name: population, dtype: float64