
pandas有Series和DataFrame兩種數(shù)據(jù)結(jié)構(gòu),我們之前已經(jīng)講過了DataFrame,接下來給大家介紹下另一種數(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)建方法
# 通過標(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
# 通過標(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
# 通過標(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添加一個空值 s["g"] = np.nan # np.nan 代表有問題的值 也會識別為空值 print("") print(s) print("") bool_index1 = s.isnull() # 判斷那些值是空值: 空值是True 非空為False print(bool_index1) print("") print(s[bool_index1]) # 取出空值 print("") bool_index2 = s.notnull() # 判斷那些值是非空值: 空值是False 非空為True 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添加一個空值 s["g"] = np.nan # np.nan 代表有問題的值 也會識別為空值 print("") print(s) print("") bool_index1 = s.isnull() # 判斷那些值是空值: 空值是True 非空為False print(bool_index1) print("") print(s[bool_index1]) # 取出空值 print("") bool_index2 = s.notnull() # 判斷那些值是非空值: 空值是False 非空為True 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添加一個空值 s["g"] = np.nan # np.nan 代表有問題的值 也會識別為空值 print("") print(s) print("") bool_index1 = s.isnull() # 判斷那些值是空值: 空值是True 非空為False print(bool_index1) print("") print(s[bool_index1]) # 取出空值 print("") bool_index2 = s.notnull() # 判斷那些值是非空值: 空值是False 非空為True 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使它符合新的索引,如果索引不存在就自動填入空值 print(s1) print("") print(s) # 不會改變原數(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) # 對應(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") # 一次刪除一個并返回副本 print(s1) print("") s2 = s.drop(["d", "e"]) # 一次刪除兩個并返回副本 print(s2) print("") print(s) # 驗證原數(shù)沒有改變
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 改為True后不會返回副本 直接修改原數(shù)組 print(s1) print("") print(s) # 驗證原數(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("") # 通過索引標(biāo)簽添加 s1["f"] = 100 print(s1) print("") # 通過append添加一個數(shù)組 并返回一個新的數(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ù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗與 t 檢驗:差異、適用場景與實踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計學(xué)領(lǐng)域,假設(shè)檢驗是驗證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點數(shù)據(jù)的科學(xué)計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點數(shù)據(jù)時的科學(xué)計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計” 與 “用戶體驗 ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅(qū)動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實踐到業(yè)務(wù)價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價值導(dǎo)向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10