京公网安备 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
在数据科学的工具箱中,析因分析(Factor Analysis, FA)、聚类分析(Clustering Analysis)与主成分分析(Principal Component ...
2025-12-18自2017年《Attention Is All You Need》一文问世以来,Transformer模型凭借自注意力机制的强大建模能力,在NLP、CV、语音等领域 ...
2025-12-18在CDA(Certified Data Analyst)数据分析师的时间序列分析工作中,常面临这样的困惑:某电商平台月度销售额增长20%,但增长是来 ...
2025-12-18在机器学习实践中,“超小数据集”(通常指样本量从几十到几百,远小于模型参数规模)是绕不开的场景——医疗领域的罕见病数据、 ...
2025-12-17数据仓库作为企业决策分析的“数据中枢”,其价值完全依赖于数据质量——若输入的是缺失、重复、不一致的“脏数据”,后续的建模 ...
2025-12-17在CDA(Certified Data Analyst)数据分析师的日常工作中,“随时间变化的数据”无处不在——零售企业的每日销售额、互联网平台 ...
2025-12-17在休闲游戏的运营体系中,次日留存率是当之无愧的“生死线”——它不仅是衡量产品核心吸引力的首个关键指标,更直接决定了后续LT ...
2025-12-16在数字化转型浪潮中,“以用户为中心”已成为企业的核心经营理念,而用户画像则是企业洞察用户、精准决策的“核心工具”。然而, ...
2025-12-16在零售行业从“流量争夺”转向“价值深耕”的演进中,塔吉特百货(Target)以两场标志性实践树立了行业标杆——2000年后的孕妇精 ...
2025-12-15在统计学领域,二项分布与卡方检验是两个高频出现的概念,二者都常用于处理离散数据,因此常被初学者混淆。但本质上,二项分布是 ...
2025-12-15在CDA(Certified Data Analyst)数据分析师的工作链路中,“标签加工”是连接原始数据与业务应用的关键环节。企业积累的用户行 ...
2025-12-15在Python开发中,HTTP请求是与外部服务交互的核心场景——调用第三方API、对接微服务、爬取数据等都离不开它。虽然requests库已 ...
2025-12-12在数据驱动决策中,“数据波动大不大”是高频问题——零售店长关心日销售额是否稳定,工厂管理者关注产品尺寸偏差是否可控,基金 ...
2025-12-12在CDA(Certified Data Analyst)数据分析师的能力矩阵中,数据查询语言(SQL)是贯穿工作全流程的“核心工具”。无论是从数据库 ...
2025-12-12很多小伙伴都在问CDA考试的问题,以下是结合 2025 年最新政策与行业动态更新的 CDA 数据分析师认证考试 Q&A,覆盖考试内容、报考 ...
2025-12-11在Excel数据可视化中,柱形图因直观展示数据差异的优势被广泛使用,而背景色设置绝非简单的“换颜色”——合理的背景色能突出核 ...
2025-12-11在科研实验、商业分析或医学研究中,我们常需要判断“两组数据的差异是真实存在,还是偶然波动”——比如“新降压药的效果是否优 ...
2025-12-11在CDA(Certified Data Analyst)数据分析师的工作体系中,数据库就像“数据仓库的核心骨架”——所有业务数据的存储、组织与提 ...
2025-12-11在神经网络模型搭建中,“最后一层是否添加激活函数”是新手常困惑的关键问题——有人照搬中间层的ReLU激活,导致回归任务输出异 ...
2025-12-05在机器学习落地过程中,“模型准确率高但不可解释”“面对数据噪声就失效”是两大核心痛点——金融风控模型若无法解释决策依据, ...
2025-12-05