
pandas有Series和DataFrame兩種數(shù)據(jù)結(jié)構(gòu),我們之前已經(jīng)講過(guò)了DataFrame,接下來(lái)給大家介紹下另一種數(shù)據(jù)結(jié)構(gòu)Series。
什么是Series?
# 自定義Series索引 arr = np.random.rand(5) s = pd.Series(arr, index=list("abcde")) print(s)
a 0.239432 b 0.554542 c 0.058231 d 0.211549 e 0.362285 dtype: float64
[ 0.67962276 0.76999562 0.95308305 0.66162424 0.93883112] 0 0.679623 1 0.769996 2 0.953083 3 0.661624 4 0.938831 dtype: float64 RangeIndex(start=0, stop=5, step=1) <class 'pandas.core.indexes.range.RangeIndex'> [0, 1, 2, 3, 4] [ 0.67962276 0.76999562 0.95308305 0.66162424 0.93883112]
# 自定義Series索引 arr = np.random.rand(5) s = pd.Series(arr, index=list("abcde")) print(s)
a 0.239432 b 0.554542 c 0.058231 d 0.211549 e 0.362285 dtype: float64
Series創(chuàng)建方法
# 通過(guò)標(biāo)量創(chuàng)建 s = pd.Series(100, index=range(5)) print(s)
0 100 1 100 2 100 3 100 4 100 dtype: int64
# 通過(guò)標(biāo)量創(chuàng)建 s = pd.Series(100, index=range(5)) print(s)
0 100 1 100 2 100 3 100 4 100 dtype: int64
# 通過(guò)標(biāo)量創(chuàng)建 s = pd.Series(100, index=range(5)) print(s)
0 100 1 100 2 100 3 100 4 100 dtype: int64
Series下標(biāo)索引
arr = np.random.rand(5)*100 s = pd.Series(arr, index=[chr(i) for i in range(97, 97+len(arr))]) print(s) print("") bool_index = s>50 # 布爾型索引 print(bool_index) print("") print(s[s>50]) # 用bool_index取出s中大于50的值
a 24.447599 b 0.795073 c 49.464825 d 9.987239 e 86.314340 dtype: float64 a False b False c False d False e True dtype: bool e 86.31434 dtype: float64
a 0.001694 b 0.107466 c 0.272233 d 0.637616 e 0.875348 dtype: float64 0.107465887721 0.107465887721 b 0.107466 d 0.637616 dtype: float64 a 0.001694 c 0.272233 dtype: float64
Series切片
print(s) s["f"] = None # 給s添加一個(gè)空值 s["g"] = np.nan # np.nan 代表有問(wèn)題的值 也會(huì)識(shí)別為空值 print("") print(s) print("") bool_index1 = s.isnull() # 判斷那些值是空值: 空值是True 非空為False print(bool_index1) print("") print(s[bool_index1]) # 取出空值 print("") bool_index2 = s.notnull() # 判斷那些值是非空值: 空值是False 非空為T(mén)rue print(bool_index2) print("") print(s[bool_index2]) # 取出非空值
a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 f None g NaN dtype: object a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 f None g NaN dtype: object a False b False c False d False e False f True g True dtype: bool f None g NaN dtype: object a True b True c True d True e True f False g False dtype: bool a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 dtype: object
Series布爾型索引
print(s) s["f"] = None # 給s添加一個(gè)空值 s["g"] = np.nan # np.nan 代表有問(wèn)題的值 也會(huì)識(shí)別為空值 print("") print(s) print("") bool_index1 = s.isnull() # 判斷那些值是空值: 空值是True 非空為False print(bool_index1) print("") print(s[bool_index1]) # 取出空值 print("") bool_index2 = s.notnull() # 判斷那些值是非空值: 空值是False 非空為T(mén)rue print(bool_index2) print("") print(s[bool_index2]) # 取出非空值
a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 f None g NaN dtype: object a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 f None g NaN dtype: object a False b False c False d False e False f True g True dtype: bool f None g NaN dtype: object a True b True c True d True e True f False g False dtype: bool a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 dtype: object
print(s) s["f"] = None # 給s添加一個(gè)空值 s["g"] = np.nan # np.nan 代表有問(wèn)題的值 也會(huì)識(shí)別為空值 print("") print(s) print("") bool_index1 = s.isnull() # 判斷那些值是空值: 空值是True 非空為False print(bool_index1) print("") print(s[bool_index1]) # 取出空值 print("") bool_index2 = s.notnull() # 判斷那些值是非空值: 空值是False 非空為T(mén)rue print(bool_index2) print("") print(s[bool_index2]) # 取出非空值
a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 f None g NaN dtype: object a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 f None g NaN dtype: object a False b False c False d False e False f True g True dtype: bool f None g NaN dtype: object a True b True c True d True e True f False g False dtype: bool a 24.4476 b 0.795073 c 49.4648 d 9.98724 e 86.3143 dtype: object
Series基本技巧
查看數(shù)據(jù)
import numpy as np import pandas as pd
s = pd.Series(np.random.rand(15)) print(s) print("") print(s.head()) # 查看前5條數(shù)據(jù) print("") print(s.head(2)) # 查看前2條數(shù)據(jù) print("") print(s.tail()) # 查看后5條數(shù)據(jù) print("") print(s.tail(2)) # 查看后兩條數(shù)據(jù)
0 0.049732 1 0.281123 2 0.398361 3 0.492084 4 0.555350 5 0.729037 6 0.603854 7 0.643413 8 0.951804 9 0.459948 10 0.261974 11 0.897656 12 0.428898 13 0.426533 14 0.301044 dtype: float64 0 0.049732 1 0.281123 2 0.398361 3 0.492084 4 0.555350 dtype: float64 0 0.049732 1 0.281123 dtype: float64 10 0.261974 11 0.897656 12 0.428898 13 0.426533 14 0.301044 dtype: float64 13 0.426533 14 0.301044 dtype: float64
重置索引
# reindex 與給索引重新命名不同 s = pd.Series(np.random.rand(5), index=list("bdeac")) print(s) print("") s1 = s.reindex(list("abcdef")) # Series的reindex使它符合新的索引,如果索引不存在就自動(dòng)填入空值 print(s1) print("") print(s) # 不會(huì)改變?cè)瓟?shù)組 print("") s2 = s.reindex(list("abcdef"), fill_value=0) # 如果索引值不存在就自定義填入缺失值 print(s2)
b 0.539124 d 0.853346 e 0.065577 a 0.406689 c 0.562758 dtype: float64 a 0.406689 b 0.539124 c 0.562758 d 0.853346 e 0.065577 f NaN dtype: float64 b 0.539124 d 0.853346 e 0.065577 a 0.406689 c 0.562758 dtype: float64 a 0.406689 b 0.539124 c 0.562758 d 0.853346 e 0.065577 f 0.000000 dtype: float64
s1 = pd.Series(np.random.rand(3), index=list("abc")) s2 = pd.Series(np.random.rand(3), index=list("cbd")) print(s1) print("") print(s2) print("") print(s1+s2) # 對(duì)應(yīng)的標(biāo)簽相加 缺失值加任何值還是缺失值
a 0.514657 b 0.618971 c 0.456840 dtype: float64 c 0.083065 b 0.893543 d 0.125063 dtype: float64 a NaN b 1.512513 c 0.539905 d NaN dtype: float64
刪除
# Series.drop("索引名") s = pd.Series(np.random.rand(5), index=list("abcde")) print(s) print("") s1 = s.drop("b") # 一次刪除一個(gè)并返回副本 print(s1) print("") s2 = s.drop(["d", "e"]) # 一次刪除兩個(gè)并返回副本 print(s2) print("") print(s) # 驗(yàn)證原數(shù)沒(méi)有改變
a 0.149823 b 0.330215 c 0.069852 d 0.967414 e 0.867417 dtype: float64 a 0.149823 c 0.069852 d 0.967414 e 0.867417 dtype: float64 a 0.149823 b 0.330215 c 0.069852 dtype: float64 a 0.149823 b 0.330215 c 0.069852 d 0.967414 e 0.867417 dtype: float64
s = pd.Series(np.random.rand(5), index=list("abcde")) print(s) print("") s1 = s.drop(["b", "c"], inplace=True) # inplace默認(rèn)是False 改為T(mén)rue后不會(huì)返回副本 直接修改原數(shù)組 print(s1) print("") print(s) # 驗(yàn)證原數(shù)組已改變
a 0.753187 b 0.077156 c 0.626230 d 0.428064 e 0.809005 dtype: float64 None a 0.753187 d 0.428064 e 0.809005 dtype: float64
添加
s1 = pd.Series(np.random.rand(5), index=list("abcde")) print(s1) print("") # 通過(guò)索引標(biāo)簽添加 s1["f"] = 100 print(s1) print("") # 通過(guò)append添加一個(gè)數(shù)組 并返回一個(gè)新的數(shù)組 s2 = s1.append(pd.Series(np.random.rand(2), index=list("mn"))) print(s2)
a 0.860190 b 0.351980 c 0.237463 d 0.159595 e 0.119875 dtype: float64 a 0.860190 b 0.351980 c 0.237463 d 0.159595 e 0.119875 f 100.000000 dtype: float64 a 0.860190 b 0.351980 c 0.237463 d 0.159595 e 0.119875 f 100.000000 m 0.983410 n 0.293722 dtype: float64
數(shù)據(jù)分析咨詢(xún)請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶(hù) ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱(chēng) BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢(xún)到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢(xún)結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢(xún)結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專(zhuān)注于從單 ...
2025-07-09year_month數(shù)據(jù)類(lèi)型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類(lèi)型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門(mén)控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書(shū)考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專(zhuān)業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開(kāi)啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03