前言
大家好!本期跟大家分享的知識是 Pandas 數(shù)據(jù)結構—Series。
一、Series的創(chuàng)建
Series 是一種類似于一維數(shù)組的對象,由下面兩部分組成:
-
values
:一組數(shù)據(jù),ndarray 類型 -
index
:數(shù)據(jù)索引
顧名思義,我們在創(chuàng)建 Series
對象時,需要傳遞一組數(shù)據(jù),該數(shù)據(jù)大多數(shù)時候是可迭代對象。因此,下面三種創(chuàng)建方式都是將數(shù)據(jù)傳入到 Series 方法中。
1.1 列表數(shù)組創(chuàng)建
以列表作為數(shù)據(jù)創(chuàng)建 Series。
list1 = list('ABCD') # 創(chuàng)建列表
s =pd.Series(list1) # 傳遞列表數(shù)據(jù)到 Series 方法中
print(s)
print(type(s.values))
print(type(s.index))
###########結果###########
0 A
1 B
2 C
3 D
dtype: object
<class 'numpy.ndarray'>
<class 'pandas.core.indexes.range.RangeIndex'>
以數(shù)組作為數(shù)據(jù)創(chuàng)建 Series。
n = np.array(range(5,10))
s2 = pd.Series(n)
s2
###########結果###########
0 5
1 6
2 7
3 8
4 9
dtype: int32
1.2 字典創(chuàng)建
前兩種方式,都是只傳遞了數(shù)據(jù),那么索引是默認索引(0 ~ N-1);下面的字典創(chuàng)建方式,則是以字典的鍵為索引,字典的值為數(shù)據(jù)。
d = {
'a':11,'b':22,'c':33,'d':44
}
s = pd.Series(d)
s
###########結果###########
a 11
b 22
c 33
d 44
dtype: int64
1.3 通過標量創(chuàng)建
s = pd.Series(100,index=range(5))
s
###########結果###########
0 100
1 100
2 100
3 100
4 100
dtype: int64
二、Series索引
從以上 Series 的創(chuàng)建中我們可以看出,Series 的索引是可以修改的
。我們先來探討以下索引的作用。
-
獲取元素:有多種獲取方式,s.索引名,s[‘索引名’],s.loc[‘索引名’]
-
允許修改:(為
s.index
重新賦值即可,注意前后數(shù)量一致)
顯式即表示使用索引名稱的方式,隱式即表示使用序號的方式。后面的顯式切片和隱式切片也是同理。
2.1 顯式索引
取單個值時,三種方式:(假設 Series
對象名為 s
)
- s.索引名(數(shù)字索引不能用這種方式)
- s[‘索引名’]
- s.loc[‘索引名’]
取多個值時,返回一個新的 Series 對象,兩種方式(也就是加中括號):
- s.[[‘索引名1’,‘索引名2’]]
- s.loc[[‘索引名1’,‘索引名2’]]
s = pd.Series(np.array(range(5,10)),index=list('abcde'),name='number')
print(s)
display(s.a,s['a'],s.loc['a'])
###########結果###########
a 5
b 6
c 7
d 8
e 9
Name: number, dtype: int32
5
5
5
2.2 隱式索引
隱式索引和顯示索引的區(qū)別就是它通過數(shù)字來獲取值。因為是數(shù)字,因此 s.number
這種方式肯定 不能用了 ,其他都相同。
取單值,兩種方式:
- s[number]
- s.iloc[number]
取多值,兩種方式:
- s[[number1,number2]]
- s.iloc[[number1,number2]]
s = pd.Series(np.array(range(5,10)),index=list('abcde'),name='number')
print(s)
print('取單值')
print(s[1])
print(s.iloc[1])
print('取多值')
print(s[[1,2]])
print(s.iloc[[1,2]])
###########結果###########
a 5
b 6
c 7
d 8
e 9
Name: number, dtype: int32
a 5
b 6
c 7
d 8
e 9
Name: number, dtype: int32
取單值
6
6
取多值
b 6
c 7
Name: number, dtype: int32
b 6
c 7
Name: number, dtype: int32
三、Series切片
切片操作是獲取一個新的 Series 對象的操作,顯式切片是為左閉右閉,隱式切片時為左閉右開。
2.1 顯式切片
兩種方式:
- s[索引名1:索引名2]
- s.loc[索引名1:索引名2]
2.2 隱式切片
兩種方式:
- s[number1:number2]
- s.iloc[number1:number2]
s = pd.Series({
'yw':100,
'math':150,
'eng':110,
'Python':130
})
print('數(shù)據(jù):',end='')
print('-'*10)
print(s)
print('-'*10)
print('顯式切片')
print(s['yw':'math'])
print('-'*10)
print(s.loc['yw':'math'])
print('-'*10)
print('隱式切片')
print(s[0:1])
print('-'*10)
print(s.iloc[0:1])
###########結果###########
數(shù)據(jù):----------
yw 100
math 150
eng 110
Python 130
dtype: int64
----------
顯式切片
yw 100
math 150
dtype: int64
----------
yw 100
math 150
dtype: int64
----------
隱式切片
yw 100
dtype: int64
----------
yw 100
dtype: int64
四、Series基本屬性和方法
Series基本屬性和方法是讓我們更好了解數(shù)據(jù)組成的手段。
4.1 屬性
屬性 | 作用 |
---|---|
s.shape | 查看數(shù)據(jù)行列 |
s.ndim | 查看維度,Series 就是一維,ndim 恒等于1 |
s.size | 查看數(shù)據(jù)總數(shù) |
s.index | 查看索引 |
s.values | 查看數(shù)據(jù) |
s.name | 查看 Series 對象的 name,若未設定則為空 |
4.2 方法
方法 | 功能 |
---|---|
s.head() | 查看前5條數(shù)據(jù),若傳入數(shù)字 n ,則查看前 n 條 |
s.tail() | 查看后5條數(shù)據(jù),若傳入數(shù)字 n ,則查看后 n 條 |
s.isnull() | 判斷數(shù)據(jù)是否為空,空的為 True ,不空的為 False |
s.notnull() | 判斷數(shù)據(jù)是否不空,空的為 False ,不空的為 True |
4.3 案例——使用 bool 值去除空值
原理:Series 切片可以再傳入一個 Series 對象,該 Series 對象索引要和原來相同,那么值為 False 的將不會被取出。
請看示例:
s = pd.Series(['a','e','f','b'])
s[pd.Series([True,True,False,True])]
###########結果###########
0 a
1 e
3 b
dtype: object
如果改為:
s = pd.Series(['a','e','f','b'])
s[pd.Series([True,True,False,True],index=list('abcd'))]
###########結果###########
那么將會報錯:
IndexingError Traceback (most recent call last)
E:\Temp/ipykernel_15804/3537358820.py in <module>
1 s = pd.Series(['a','e','f','b'])
----> 2 s[pd.Series([True,True,False,True],index=list('abcd'))]
D:\PF\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
1001
1002 if com.is_bool_indexer(key):
-> 1003 key = check_bool_indexer(self.index, key)
1004 key = np.asarray(key, dtype=bool)
1005 return self._get_values(key)
D:\PF\Anaconda3\lib\site-packages\pandas\core\indexing.py in check_bool_indexer(index, key)
2550 indexer = result.index.get_indexer_for(index)
2551 if -1 in indexer:
-> 2552 raise IndexingError(
2553 "Unalignable boolean Series provided as "
2554 "indexer (index of the boolean Series and of "
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
下面是 Series 去除空值的案例。
s = pd.Series(['zhangsan','lisi','a',np.NAN,None])
print('數(shù)據(jù):'+'-'*10)
print(s)
conditon = s.isnull()
print('判空情況:'+'-'*10)
print(conditon)
# 使用 bool 值索引過濾數(shù)據(jù)
s = s[~conditon]
print('過濾結果:'+'-'*10)
print(s)
###########結果###########
數(shù)據(jù):----------
0 zhangsan
1 lisi
2 a
3 NaN
4 None
dtype: object
判空情況:----------
0 False
1 False
2 False
3 True
4 True
dtype: bool
過濾結果:----------
0 zhangsan
1 lisi
2 a
dtype: object
五、Series運算
Series 運算包括算術運算和 Series 對象之間運算。算術運算是針對每一個元素的,有 +、-、*、/、 //、 %、 ** 等,這里不再贅述。Series 對象間的運算,只要記住,索引一個有一個沒有時,計算值為 NaN
,其他按照算術運算計算即可。
- 算術運算
s = pd.Series(np.array(range(5,10)))
print(s)
s * 10
###########結果###########
0 5
1 6
2 7
3 8
4 9
dtype: int32
0 50
1 60
2 70
3 80
4 90
dtype: int32
- Series對象間的運算
s1 = pd.Series(np.array(range(5,10)))
s2 = pd.Series([3,6,10,12])
print(s1)
print(s2)
s1 + s2 # 索引一個有一個沒有時,計算值為 NaN
###########結果###########
0 5
1 6
2 7
3 8
4 9
dtype: int32
0 3
1 6
2 10
3 12
dtype: int64
0 8.0
1 12.0
2 17.0
3 20.0
4 NaN
dtype: float64
六、Series多層行索引
6.1 Series多層索引的構建
Series 不僅支持單層索引,還支持多層索引。最簡單的實現(xiàn)方式就是將 index
設置成多維。
下面以二級行索引為例:
s = pd.Series(np.random.randint(60,100,6),index=[['語文','語文','語文','數(shù)學','數(shù)學','數(shù)學'],['小明','小紅','小麗','小明','小紅','小麗']])
print(s)
###########結果###########
語文 小明 90
小紅 72
小麗 97
數(shù)學 小明 81
小紅 74
小麗 84
dtype: int32
6.2 Series多層索引的索引和切片操作
對于 Series
多層索引的索引和切片操作,只要記住以下兩點:
- 要先取第一層,再取第二層,不能直接取第二層索引
- 獲取到第一層之后,就是一個普通的單層索引 Series
- 隱式索引,直接得到數(shù)
具體的方式,還是索引和切片都分為顯式和隱式,下面通過一個案例來演示。
索引:
s = pd.Series(np.random.randint(60,100,6),index=[['語文','語文','語文','數(shù)學','數(shù)學','數(shù)學'],['小明','小紅','小麗','小明','小紅','小麗']])
print(s)
# 索引
print('顯式索引:'+'-'*10)
print(s['語文']) # 獲取到單層 Series
print(s.loc['語文'])
print(s['語文']['小明'],s.loc['語文']['小明']) # 獲取到單個值
print('隱式索引:'+'-'*10)
print(s.iloc[0])
print(s[0]) # 獲取到單個值
###########結果###########
語文 小明 94
小紅 95
小麗 60
數(shù)學 小明 66
小紅 84
小麗 76
dtype: int32
顯式索引:----------
小明 94
小紅 95
小麗 60
dtype: int32
小明 94
小紅 95
小麗 60
dtype: int32
94 94
隱式索引:----------
94
94
切片:
s = pd.Series(np.random.randint(60,100,6),index=[['語文','語文','語文','數(shù)學','數(shù)學','數(shù)學'],['小明','小紅','小麗','小明','小紅','小麗']])
s = s.sort_index()
print(s)
print('顯式切片'+'-'*10)
print(s['數(shù)學':'語文'])
print(s.loc['數(shù)學':'語文'])
print('隱式切片'+'-'*10)
print(s[0:2])
print(s.iloc[0:2])
###########結果###########
數(shù)學 小麗 67
小明 64
小紅 92
語文 小麗 84
小明 99
小紅 82
dtype: int32
顯式切片----------
數(shù)學 小麗 67
小明 64
小紅 92
語文 小麗 84
小明 99
小紅 82
dtype: int32
數(shù)學 小麗 67
小明 64
小紅 92
語文 小麗 84
小明 99
小紅 82
dtype: int32
隱式切片----------
數(shù)學 小麗 67
小明 64
dtype: int32
數(shù)學 小麗 67
小明 64
dtype: int32
結語
?? 本期跟大家分享的 “芝士” 就到此結束了,關于 Series 數(shù)據(jù)結構,你學會了嗎??
?? 我是南晨曦,在學習的路上一直前行,期待與你一起進步。~ ??
??如果文中有些地方不清楚的話,歡迎聯(lián)系我,我會給大家提供思路及解答。??文章來源:http://www.zghlxwxcb.cn/news/detail-635023.html
參考文檔
python數(shù)據(jù)分析:Pandas之Series文章來源地址http://www.zghlxwxcb.cn/news/detail-635023.html
到了這里,關于【數(shù)據(jù)分析專欄之Python篇】五、pandas數(shù)據(jù)結構之Series的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!