京公网安备 11010802034615号
经营许可证编号:京B2-20210330
pandas有Series和DataFrame两种数据结构,我们之前已经讲过了DataFrame,接下来给大家介绍下另一种数据结构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创建方法
# 通过标量创建 s = pd.Series(100, index=range(5)) print(s)
0 100 1 100 2 100 3 100 4 100 dtype: int64
# 通过标量创建 s = pd.Series(100, index=range(5)) print(s)
0 100 1 100 2 100 3 100 4 100 dtype: int64
# 通过标量创建 s = pd.Series(100, index=range(5)) print(s)
0 100 1 100 2 100 3 100 4 100 dtype: int64
Series下标索引
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基本技巧
查看数据
import numpy as np import pandas as pd
s = pd.Series(np.random.rand(15)) print(s) print("") print(s.head()) # 查看前5条数据 print("") print(s.head(2)) # 查看前2条数据 print("") print(s.tail()) # 查看后5条数据 print("") print(s.tail(2)) # 查看后两条数据
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) # 不会改变原数组 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) # 对应的标签相加 缺失值加任何值还是缺失值
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) # 验证原数没有改变
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默认是False 改为True后不会返回副本 直接修改原数组 print(s1) print("") print(s) # 验证原数组已改变
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("") # 通过索引标签添加 s1["f"] = 100 print(s1) print("") # 通过append添加一个数组 并返回一个新的数组 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
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
日常用Excel做数据管理、台账维护、报表整理时,添加备注列是高频操作——用来标注异常、说明业务背景、记录处理进度、补充关键 ...
2026-03-23作为业内主流的自助式数据可视化工具,Tableau凭借拖拽式操作、强大的数据联动能力、灵活的仪表板搭建,成为数据分析师、业务人 ...
2026-03-23在CDA(Certified Data Analyst)数据分析师的日常工作与认证考核中,分类变量的关联分析是高频核心场景。用户性别是否影响商品 ...
2026-03-23在数据工作的全流程中,数据清洗是最基础、最耗时,同时也是最关键的核心环节,无论后续是做常规数据分析、可视化报表,还是开展 ...
2026-03-20在大数据与数据驱动决策的当下,“数据分析”与“数据挖掘”是高频出现的两个核心概念,也是很多职场人、入门学习者容易混淆的术 ...
2026-03-20在CDA(Certified Data Analyst)数据分析师的全流程工作闭环中,统计制图是连接严谨统计分析与高效业务沟通的关键纽带,更是CDA ...
2026-03-20在MySQL数据库优化中,分区表是处理海量数据的核心手段——通过将大表按分区键(如时间、地域、ID范围)分割为多个独立的小分区 ...
2026-03-19在商业智能与数据可视化领域,同比、环比增长率是分析数据变化趋势的核心指标——同比(YoY)聚焦“长期趋势”,通过当前周期与 ...
2026-03-19在数据分析与建模领域,流传着一句行业共识:“数据决定上限,特征决定下限”。对CDA(Certified Data Analyst)数据分析师而言 ...
2026-03-19机器学习算法工程的核心价值,在于将理论算法转化为可落地、可复用、高可靠的工程化解决方案,解决实际业务中的痛点问题。不同于 ...
2026-03-18在动态系统状态估计与目标跟踪领域,高精度、高鲁棒性的状态感知是机器人导航、自动驾驶、工业控制、目标检测等场景的核心需求。 ...
2026-03-18“垃圾数据进,垃圾结果出”,这是数据分析领域的黄金法则,更是CDA(Certified Data Analyst)数据分析师日常工作中时刻恪守的 ...
2026-03-18在机器学习建模中,决策树模型因其结构直观、易于理解、无需复杂数据预处理等优势,成为分类与回归任务的首选工具之一。而变量重 ...
2026-03-17在数据分析中,卡方检验是一类基于卡方分布的假设检验方法,核心用于分析分类变量之间的关联关系或实际观测分布与理论期望分布的 ...
2026-03-17在数字化转型的浪潮中,企业积累的数据日益庞大且分散——用户数据散落在注册系统、APP日志、客服记录中,订单数据分散在交易平 ...
2026-03-17在数字化时代,数据分析已成为企业决策、业务优化、增长突破的核心支撑,从数据仓库搭建(如维度表与事实表的设计)、数据采集清 ...
2026-03-16在数据仓库建设、数据分析(尤其是用户行为分析、业务指标分析)的实践中,维度表与事实表是两大核心组件,二者相互依存、缺一不 ...
2026-03-16数据是CDA(Certified Data Analyst)数据分析师开展一切工作的核心载体,而数据读取作为数据生命周期的关键环节,是连接原始数 ...
2026-03-16在用户行为分析实践中,很多从业者会陷入一个核心误区:过度关注“当前数据的分析结果”,却忽视了结果的“泛化能力”——即分析 ...
2026-03-13在数字经济时代,用户的每一次点击、浏览、停留、转化,都在传递着真实的需求信号。用户行为分析,本质上是通过收集、整理、挖掘 ...
2026-03-13