日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Pandas數據結構之Series的使用_python

作者:Jackson_Wang ? 更新時間: 2022-06-02 編程語言

一. Series 簡介

Series是一種類似于一維數組的對象,是由一組數據(各種NumPy數據類型)以及一組與之相關的數據標簽(即索引)組成。僅由一組數據也可產生簡單的Series對象

Series 總的來說就是帶標簽的一維數組,可存儲整數、浮點數、字符串、Python對象等類型的數據。標簽軸通常叫做索引。

二. 實例化 Series

2.1 使用一維數組實例化

用一維數組實例化Series時,索引長度必須與數組長度一致。沒有指定索引時,Pandas會幫我們創建默認的數值型索引。

In [1]: s1 = pd.Series([1, 2, 3, 4])
Out[1]:
0	1
1	2
2	3
3	4
dtype: int64

In [2]: s2 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
Out[2]:
a	1
b	2
c	3
d	4
dtype: int64

注意: Pandas 是支持重復索引的。但我們也可以重置索引,具體操作方法在后續章節中會給出。

2.2 使用字典實例化

使用字典實例化Series時, 如果未傳入索引,則索引的值為字典的key:

In [1]: pd.Series({'i': 0, 'j': 1, 'k': 2})
Out[1]: 
i    0
j    1
k    2
dtype: int64

2.3 使用標量例化

使用標量值實例化時,必須提供索引。Series 按索引長度重復該標量值。

In [1]: pd.Series(6, index=[0, 1, 2])
Out[1]: 
0    6
1    6
2    6
dtype: int64

三.Series 簡單使用

3.1 為Series添加Name屬性

在實例化Series時,可以傳入name參數為Series添加name屬性。同時,Seires也支持重命名:

In [1]: s = pd.Series(6, index=[0, 1, 2], name='six')
Out[1]: 
0    6
1    6
2    6
Name: six, dtype: int64

In [2]: s.name
Out[2]: 'six'

In [3]: s = s.rename('sixsixsix')
In [4]: s.name
Out[4]: 'sixsixsix'

3.2 基于位置的切片

Series提供了類似于Python列表的切片方式:

In[0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[1]: s[0:2] 	#取下標為0和1的兩個數據(不包括2,也就是從第一個開始取,取兩個數據)
Out[1]:
a    1
b    2
dtype: int64

In[2]: s[:3] 	#取前三個數據
Out[2]:
a    1
b    2
c    3
dtype: int64

In[3]: s[-2:] 	#取最后兩個數據(也可以理解為從倒查第二個數據一直取到末尾)
Out[3]:
c    3
d    4
dtype: int64

In[4]: s[[0,2,3]] 	#取第1、3、4這個三個數據(注意下標是從0開始的,轉換為位置時需+1)
Out[4]:
a    1
c    3
d    4
dtype: int64		#注意:如果輸入的位置大于列表的長度則會報出“indexers are out-of-bounds”異常

3.3 基于索引的切片

Series可使用索引標簽的值來提取值:

In [0]:s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In [1]: s['a'] 	#提取s中,標簽為a的值
Out[1]:
a    1
dtype: int64

In [1]: s[['a', 'b', 'c']] 	#提取s中,標簽為a, b, c的值
Out[1]:
a    1
b    2
c    3
dtype: int64

如果傳入的索引標簽的值不在Seires的軸索引中,那將會報 KeyError 異常,這里建議大家使用Series的 get 方法獲取值,如果不存在,則會返回None,同時也可設置default參數,用于不存在時的默認返回值。

In [0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In [1]: s['f'] 	#提取s中,標簽為f的值, f不存在,將會報出異常
Out[1]:KeyError

In [2]:s.get('f') #提取s中,標簽為f的值, 若f不存在,默認返回None
Out[2]:None

In [3]:s.get('f'. default=-1) #提取s中,標簽為f的值, 若f不存在,返回-1
Out[3]:-1

3.4 基于條件的切片

In[0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[1]: s[s < 2] 	#提取s中,小于2的值
Out[1]:
a    1
b    2
dtype: int64

In[1]: s[s> s.mean()] 	#提取s中,大于平均數的值
Out[1]:
c    3
d    4
dtype: int64

In[1]: s[s.between(1, 3, inclusive=False)] 	#提取s中,值介于1,3之間的數據(不包含1,3)
Out[1]:
b    2
dtype: int64

在提取區間數據時,如果想讓兩端的值包含其中(滿足兩端的值也被提取出來),只需要把 inclusive 參數的值賦為True

3.5 其他操作

Series 不用循環也可以像操作單個數值一樣快速進行數學運算:

In[0]: s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[1]: s + s
Out[1]:
a    2
b    4
c    6
d    8
dtype: int64

In[2]: s - 1
Out[2]:
a    0
b    1
c    2
d    3
dtype: int64

Series 之間的操作會自動 基于標簽 對齊數據. 如果一個Series中的標簽在另一個Series中不存在,那么計算得到的結果將是NaN,即缺失值,有缺失值NaN的處理在后續章節也會講到。因此,我們不用顧及執行操作的Series是否有相同的標簽。 Pandas數據結構集成的數據對齊的功能,是Pandas區別于大多數標簽型數據處理工具的重要特性。

In[0]: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
In[0]: s2 = pd.Series([3, 6, 11], index=['a', 'b', 'f'])
In[1]: s1 + s2
Out[1]:
a   4.0
b   8.0
c   NaN
d   NaN
f   NaN
dtype: float64

原文鏈接:https://juejin.cn/post/6926468320619298830

欄目分類
最近更新