系列(Series)是能夠保存任何類型的數(shù)據(jù)(整數(shù),字符串,浮點(diǎn)數(shù),Python對象等)的一維標(biāo)記數(shù)組。軸標(biāo)簽統(tǒng)稱為索引。
Pandas系列可以使用以下構(gòu)造函數(shù)創(chuàng)建 -
pandas.Series( data, index, dtype, copy)。
構(gòu)造函數(shù)的參數(shù)如下 -
| 編號 | 參數(shù) | 描述 |
|---|---|---|
| 1 | data |
數(shù)據(jù)采取各種形式,如:ndarray,list,constants |
| 2 | index |
索引值必須是唯一的和散列的,與數(shù)據(jù)的長度相同。 默認(rèn)np.arange(n)如果沒有索引被傳遞。 |
| 3 | dtype |
dtype用于數(shù)據(jù)類型。如果沒有,將推斷數(shù)據(jù)類型 |
| 4 | copy |
復(fù)制數(shù)據(jù),默認(rèn)為false。 |
可以使用各種輸入創(chuàng)建一個系列,如 -
創(chuàng)建一個基本系列是一個空系列。
示例
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print s
執(zhí)行上面示例代碼,輸出結(jié)果如下 -
Series([], dtype: float64)
如果數(shù)據(jù)是ndarray,則傳遞的索引必須具有相同的長度。 如果沒有傳遞索引值,那么默認(rèn)的索引將是范圍(n),其中n是數(shù)組長度,即[0,1,2,3…. range(len(array))-1] - 1]。
示例1
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print s
執(zhí)行上面示例代碼,輸出結(jié)果如下 -
0 a
1 b
2 c
3 d
dtype: object
這里沒有傳遞任何索引,因此默認(rèn)情況下,它分配了從0到len(data)-1的索引,即:0到3。
示例2
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
print s
執(zhí)行上面示例代碼,輸出結(jié)果如下 -
100 a
101 b
102 c
103 d
dtype: object
在這里傳遞了索引值?,F(xiàn)在可以在輸出中看到自定義的索引值。
字典(dict)可以作為輸入傳遞,如果沒有指定索引,則按排序順序取得字典鍵以構(gòu)造索引。 如果傳遞了索引,索引中與標(biāo)簽對應(yīng)的數(shù)據(jù)中的值將被拉出。
示例2
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data)
print s
執(zhí)行上面示例代碼,輸出結(jié)果如下 -
a 0.0
b 1.0
c 2.0
dtype: float64
注意 - 字典鍵用于構(gòu)建索引。
示例
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data,index=['b','c','d','a'])
print s
執(zhí)行上面示例代碼,輸出結(jié)果如下 -
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
注意觀察 - 索引順序保持不變,缺少的元素使用NaN(不是數(shù)字)填充。
如果數(shù)據(jù)是標(biāo)量值,則必須提供索引。將重復(fù)該值以匹配索引的長度。
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print s
執(zhí)行上面示例代碼,得到以下結(jié)果 -
0 5
1 5
2 5
3 5
dtype: int64
系列中的數(shù)據(jù)可以使用類似于訪問ndarray中的數(shù)據(jù)來訪問。
示例-1
檢索第一個元素。比如已經(jīng)知道數(shù)組從零開始計數(shù),第一個元素存儲在零位置等等。
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the first element
print s[0]
執(zhí)行上面示例,得到以下結(jié)果 -
1
示例-2
檢索系列中的前三個元素。 如果a:被插入到其前面,則將從該索引向前的所有項目被提取。 如果使用兩個參數(shù)(使用它們之間),兩個索引之間的項目(不包括停止索引)。
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the first three element
print s[:3]
執(zhí)行上面示例,得到以下結(jié)果 -
a 1
b 2
c 3
dtype: int64
示例-3
檢索最后三個元素,參考以下示例代碼 -
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the last three element
print s[-3:]
執(zhí)行上面示例代碼,得到以下結(jié)果 -
c 3
d 4
e 5
dtype: int64
一個系列就像一個固定大小的字典,可以通過索引標(biāo)簽獲取和設(shè)置值。
示例1
使用索引標(biāo)簽值檢索單個元素。
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve a single element
print s['a']
執(zhí)行上面示例代碼,得到以下結(jié)果 -
1
示例2
使用索引標(biāo)簽值列表檢索多個元素。
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve multiple elements
print s[['a','c','d']]
執(zhí)行上面示例代碼,得到以下結(jié)果 -
a 1
c 3
d 4
dtype: int64
示例3
如果不包含標(biāo)簽,則會出現(xiàn)異常。
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve multiple elements
print s['f']
執(zhí)行上面示例代碼,得到以下結(jié)果 -
…
KeyError: 'f'