国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【數(shù)據(jù)分析專欄之Python篇】五、pandas數(shù)據(jù)結構之Series

這篇具有很好參考價值的文章主要介紹了【數(shù)據(jù)分析專欄之Python篇】五、pandas數(shù)據(jù)結構之Series。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

大家好!本期跟大家分享的知識是 Pandas 數(shù)據(jù)結構—Series。

一、Series的創(chuàng)建

Series 是一種類似于一維數(shù)組的對象,由下面兩部分組成:

  • values:一組數(shù)據(jù),ndarray 類型
  • index:數(shù)據(jù)索引

【數(shù)據(jù)分析專欄之Python篇】五、pandas數(shù)據(jù)結構之Series,【數(shù)據(jù)分析專欄之Python篇】,python,pandas

顧名思義,我們在創(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)系我,我會給大家提供思路及解答。??

參考文檔

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 數(shù)據(jù)分析Pandas專欄---第一章<數(shù)據(jù)清洗>

    當我們使用爬蟲從網(wǎng)上收集到大量的數(shù)據(jù)時,經(jīng)常會面臨一個重要任務:對這些數(shù)據(jù)進行清洗和整理,以便進一步分析和利用。在Python中,pandas是一個功能強大且廣泛使用的數(shù)據(jù)處理庫,它提供了各種靈活而高效的工具,可以方便地進行數(shù)據(jù)清洗和轉換。本篇文章將重點介紹

    2024年02月21日
    瀏覽(25)
  • 數(shù)據(jù)分析Pandas專欄---第三章<Pandas合并list和字典>

    在處理數(shù)據(jù)時,經(jīng)常會遇到需要操作和轉換DataFrame的場景。特別是當涉及到從單個或多個字典合成DataFrame,以及合并多個DataFrame時,適當?shù)姆椒ê图记煽梢詷O大地簡化程序邏輯并提高代碼的可讀性與效率。此外,數(shù)據(jù)操作過程中,索引的正確管理是保持數(shù)據(jù)完整性的關鍵。本

    2024年02月21日
    瀏覽(84)
  • 【Python爬蟲與數(shù)據(jù)分析】基本數(shù)據(jù)結構

    目錄 一、概述 二、特性 三、列表 四、字典 Python基本數(shù)據(jù)結構有四種,分別是列表、元組、集合、字典 ,這是Python解釋器默認的數(shù)據(jù)結構,可以直接使用,無需像C語言那樣需要手搓或像C++那樣需要聲明STL頭文件。 Python的數(shù)據(jù)結構非常靈活,對數(shù)據(jù)類型沒有限制,即一個數(shù)

    2024年02月11日
    瀏覽(23)
  • Python基礎知識詳解:數(shù)據(jù)類型、對象結構、運算符完整分析

    Python提供了豐富的數(shù)據(jù)類型,讓我們可以靈活地處理各種數(shù)據(jù)。 首先是數(shù)值類型。數(shù)值類型包括整型、浮點型和復數(shù)。 整型(int)用于表示整數(shù),例如年齡、數(shù)量等。我們可以直接將一個整數(shù)賦值給一個變量,如下所示: 浮點型(float)用于表示帶有小數(shù)點的數(shù),例如長度

    2024年02月09日
    瀏覽(23)
  • Python 潮流周刊第 43 期(摘要),贈書 5 本《Python數(shù)據(jù)結構與算法分析(第3版)》

    本周刊由 Python貓 出品,精心篩選國內外的 250+ 信息源,為你挑選最值得分享的文章、教程、開源項目、軟件工具、播客和視頻、熱門話題等內容。愿景:幫助所有讀者精進 Python 技術,并增長職業(yè)和副業(yè)的收入。 周刊全文:https://pythoncat.top/posts/2024-03-23-weekly 特別提醒:本期

    2024年03月24日
    瀏覽(21)
  • 《玩轉Python數(shù)據(jù)分析專欄》大綱

    歡迎來到《玩轉Python數(shù)據(jù)分析分類專欄》!在這個專欄中,我們將帶您深入探索數(shù)據(jù)分析的世界,以Python為工具,解析各個領域的實際應用場景。通過60多篇教程,我們將逐步引領您從入門級到高級,從基礎知識到實戰(zhàn)技巧,助您成為一名優(yōu)秀的數(shù)據(jù)分析師。 本專欄旨在幫助

    2024年02月13日
    瀏覽(57)
  • Python數(shù)據(jù)分析-Pandas

    個人筆跡,建議不看 Series類型 DataFrame類型 是一個二維結構,類似于一張excel表 DateFrame只要求每列的數(shù)據(jù)類型相同就可以了 查看數(shù)據(jù) 讀取數(shù)據(jù)及數(shù)據(jù)操作 行操作 條件選擇 缺失值及異常值處理 判斷缺失值: 填充缺失值: 刪除缺失值 age count 2.000000 mean 1.500000 std 0.707107 min 1

    2024年02月10日
    瀏覽(33)
  • python-數(shù)據(jù)分析-pandas

    第一種:通過標量創(chuàng)建Series 第二種:通過列表創(chuàng)建Series 第三種:通過字典創(chuàng)建Series 第四種:通過ndarray創(chuàng)建Series values和index 索引和切片 第一種:通過一維列表構成的字典創(chuàng)建DataFrame 姓名 數(shù)學 語文 計算機 0 張三 87 54 34 1 李四 45 76 56 2 王五 34 55 77 3 趙六 98 90 87 姓名 數(shù)學 語文

    2023年04月23日
    瀏覽(27)
  • 【Python練習】數(shù)據(jù)分析庫Pandas

    1. 了解Serie

    2024年02月09日
    瀏覽(39)
  • 實戰(zhàn)演練Python數(shù)據(jù)分析[pandas]

    實戰(zhàn)演練Python數(shù)據(jù)分析[pandas]

    本篇文章出自于《利用Python進行數(shù)據(jù)分析》示例數(shù)據(jù) 請結合提供的示例數(shù)據(jù),分析代碼的功能,并進行數(shù)據(jù)分析與可視化拓展。本篇文章通過四個例子,通過MoviesLens數(shù)據(jù)集、美國1880-2010年的嬰兒名字、美國農(nóng)業(yè)部視頻數(shù)據(jù)庫、2012年聯(lián)邦選舉委員會數(shù)據(jù)庫來進行著重講解。

    2024年02月15日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包