數(shù)據(jù)結(jié)構(gòu)
1. Series
Series簡介及創(chuàng)建
Series創(chuàng)建帶標(biāo)簽的一維數(shù)組岩臣,其中可以包含任意數(shù)據(jù)類型(整數(shù),字符串枚碗,浮點(diǎn)數(shù)及python對象等等)襟齿。軸標(biāo)簽統(tǒng)稱為index∷逗基本創(chuàng)建方法如下:
s = pd.Series(data, index=index)
- data是數(shù)據(jù)源院水,可以是Python字典類型,ndarray或者標(biāo)量值简十。
- index代表軸標(biāo)簽檬某,傳遞列表類型。
下面根據(jù)data所傳遞的類型分3中情況考慮:
-
ndarray
如果是ndarray類型螟蝙,那么index列表的大小必須與ndarray一致恢恼。如果省略index,那么默認(rèn)的標(biāo)簽列表是:[0, 1, ..., len(data)-1].
In[4]: pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
Out[4]:
a -0.712338
b 0.275297
c -0.006178
d 1.480140
e 0.736636
dtype: float64
In[7]: pd.Series(np.random.randn(5))
Out[6]:
0 0.662331
1 -1.238960
2 -0.613474
3 1.232456
4 1.030660
dtype: float64
-
dict
如果是dict類型胰默,那么當(dāng)index提供時(shí)场斑,index列表中提供的值與字典中的鍵值相匹配的值將被創(chuàng)建漓踢,如果index列表中有dict中無法匹配的值,那么同樣會創(chuàng)建一個(gè)標(biāo)簽漏隐,其值對應(yīng)為缺失值(NaN)喧半。
如果未提供index,那么標(biāo)簽值是排序的鍵值青责。
In[8]: d = {'a':0, 'b':1, 'c':2}
In[9]: pd.Series(d)
Out[8]:
a 0
b 1
c 2
dtype: int64
In[10]: pd.Series(d, index=['a', 'b', 'c', 'd'])
Out[9]:
a 0
b 1
c 2
d NaN
dtype: float64
-
標(biāo)量值
如果data是標(biāo)量值挺据,那么必須提供標(biāo)簽值。如果標(biāo)簽值不止一個(gè)脖隶,那么標(biāo)量值將重復(fù)擴(kuò)展到標(biāo)簽的長度以匹配標(biāo)簽扁耐。
In [2]:pd.Series(2, index=['a', 'b', 'c', 'd', 'e'])
Out[2]:
a 2
b 2
c 2
d 2
e 2
dtype: int64
Series與ndarray相似
Series與ndarray的用法相似,而且大部分的Numpy庫中的函數(shù)對Series有效产阱。
In [8]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
In [9]: s[0]
Out[9]: -2.7201811094132102
In [10]: s[:3]
Out[10]:
a -2.720181
b -0.119742
c 2.032580
dtype: float64
In [11]: s[s > s.median()]
Out[11]:
c 2.032580
d 0.557399
dtype: float64
In [12]: s[[4, 3, 1]]
Out[12]:
e -0.964499
d 0.557399
b -0.119742
dtype: float64
In [13]: np.exp(s)
Out[13]:
a 0.065863
b 0.887149
c 7.633759
d 1.746125
e 0.381174
dtype: float64
Series與字典相似
Series好似一個(gè)固定大小的字典婉称,你可以通過索引來讀寫值。
In [14]: s['a']
Out[14]: -2.7201811094132102
In [15]: s['e'] = 12
In [16]: s
Out[16]:
a -2.720181
b -0.119742
c 2.032580
d 0.557399
e 12.000000
dtype: float64
In [17]: 'e' in s
Out[17]: True
In [18]: 'f' in s
Out[18]: False
嘗試用不存在的標(biāo)簽獲取值會引發(fā)KeyError的異常构蹬,比較安全的做法是使用get方法,當(dāng)標(biāo)簽不存在時(shí)王暗,得到None或者提供的默認(rèn)值。
In [20]: s.get('f')
In [21]: s.get('f', np.nan)
Out[21]: nan
Series的向量化操作與標(biāo)簽對其
在進(jìn)行數(shù)據(jù)分析時(shí)怎燥,像原始的Numpy中的數(shù)組一樣通過循環(huán)來操作Series中的值通常是不必要的瘫筐。所以,Series像ndaary一樣支持大部分的Numpy方法:
In [22]: s + s
Out[22]:
a -5.440362
b -0.239485
c 4.065161
d 1.114798
e 24.000000
dtype: float64
In [23]: s * 2
Out[23]:
a -5.440362
b -0.239485
c 4.065161
d 1.114798
e 24.000000
dtype: float64
In [24]: np.abs(s)
Out[24]:
a 2.720181
b 0.119742
c 2.032580
d 0.557399
e 12.000000
dtype: float64
Series與ndarray一個(gè)重要的區(qū)別在于铐姚,在不同Series之間進(jìn)行操作時(shí)將依照標(biāo)簽進(jìn)行對齊,即使這些Series具有不同的標(biāo)簽肛捍。
In [25]: s[1:] + s[:-1]
Out[25]:
a NaN
b -0.239485
c 4.065161
d 1.114798
e NaN
dtype: float64
可以看到隐绵,如果Series的標(biāo)簽值不同時(shí),實(shí)際上會執(zhí)行并(union)操作拙毫。如果其中的一個(gè)Series缺失部分標(biāo)簽依许,那么這些標(biāo)簽對應(yīng)的結(jié)果是缺失值NaN.
名字屬性
Series可以進(jìn)行命名,具有name屬性:
s = pd.Series(np.random.randn(5), name='something')
s
Out[27]:
0 0.446879
1 -0.578168
2 -0.120358
3 1.614526
4 -0.538751
Name: something, dtype: float64
s.name
Out[28]: 'something'
在新版本0.18.0中缀蹄,還可以通過重命名方法重新創(chuàng)建一個(gè)新Series對象:
In [30]: s2 = s.rename("different")
In [31]: s2.name
Out[31]: 'different'
http://pandas.pydata.org/pandas-docs/stable/dsintro.html#series