第二章:Series 对象详解
📋 章节概述
本章深入讲解 Pandas Series 对象的高级用法,包括索引机制、数据对齐、缺失值处理、向量化运算等核心概念。通过本章学习,你将掌握 Series 的完整操作技能。
🎯 学习目标
- 深入理解 Series 的索引机制(位置索引 vs 标签索引)
- 掌握 Series 的数据访问和修改方法
- 理解数据对齐机制及其重要性
- 掌握缺失值的检测和处理方法
- 学会向量化运算和数学统计操作
- 掌握 Series 与 Python 数据结构、NumPy 的互操作
📊 知识结构图
Series 详解
索引机制
数据修改
数据对齐
缺失值处理
向量化运算
数据类型
互操作
iloc
位置索引
loc
标签索引
切片规则
修改单个值
批量修改
添加/删除
自动对齐
索引匹配
NaN产生
检测缺失值
删除缺失值
填充缺失值
数学运算
统计方法
累积运算
python
import pandas as pd
import numpy as np
2.1 Series 索引系统概述
Series 的索引(index)是其最核心的特性之一,它允许我们通过标签而非仅仅通过位置来访问数据。
索引类型
- 位置索引(Positional Index):从 0 开始的整数位置
- 标签索引(Label Index):用户自定义的字符串或数字标签
索引访问方式对比
Series索引访问
位置索引
iloc
标签索引
loc
直接索引
中括号
按整数位置
s.iloc
切片
s.iloc切片
按标签值
s.loc
切片
s.loc切片
标签优先
s标签
位置回退
s位置
⚠️ 重要提示 :当索引是整数时,直接索引 s[0] 会优先解释为标签索引。为避免混淆,推荐始终使用 .loc[] 和 .iloc[] 明确指定索引类型。
示例1:创建带自定义索引的 Series
python
# 场景:某电商平台 2026 年各月销售额(万元)
# 使用字符串标签作为索引,更具可读性
sales_2026 = pd.Series(
data=[120.5, 135.8, 142.3, 138.6, 155.2, 168.9, 175.4, 182.1, 165.7, 158.3, 172.5, 188.6],
index=["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
name="2026年月度销售额(万元)"
)
print("2026年月度销售额 Series:")
print(sales_2026)
print(f"\n索引类型: {type(sales_2026.index)}")
print(f"索引值: {list(sales_2026.index)}")
2026年月度销售额 Series:
1月 120.5
2月 135.8
3月 142.3
4月 138.6
5月 155.2
6月 168.9
7月 175.4
8月 182.1
9月 165.7
10月 158.3
11月 172.5
12月 188.6
Name: 2026年月度销售额(万元), dtype: float64
索引类型: <class 'pandas.Index'>
索引值: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
示例2:位置索引 iloc - 按位置访问
iloc(integer location)是基于整数位置的索引方式,与 Python 列表的索引规则完全一致:
- 从 0 开始计数
- 支持负索引(-1 表示最后一个元素)
- 切片时左闭右开
[start, end)
python
print("使用 iloc 按位置访问数据:")
print(f" iloc[0] - 第一个月(1月): {sales_2026.iloc[0]}")
print(f" iloc[5] - 第六个月(6月): {sales_2026.iloc[5]}")
print(f" iloc[-1] - 最后一个月(12月): {sales_2026.iloc[-1]}")
print(f" iloc[-3] - 倒数第三个月(10月): {sales_2026.iloc[-3]}")
使用 iloc 按位置访问数据:
iloc[0] - 第一个月(1月): 120.5
iloc[5] - 第六个月(6月): 168.9
iloc[-1] - 最后一个月(12月): 188.6
iloc[-3] - 倒数第三个月(10月): 158.3
python
print("使用 iloc 切片(左闭右开):")
print(" iloc[0:3] - 前3个月(1-3月):")
print(sales_2026.iloc[0:3])
使用 iloc 切片(左闭右开):
iloc[0:3] - 前3个月(1-3月):
1月 120.5
2月 135.8
3月 142.3
Name: 2026年月度销售额(万元), dtype: float64
python
print(" iloc[6:9] - 第7-9个月:")
print(sales_2026.iloc[6:9])
iloc[6:9] - 第7-9个月:
7月 175.4
8月 182.1
9月 165.7
Name: 2026年月度销售额(万元), dtype: float64
python
print(" iloc[-4:] - 最后4个月:")
print(sales_2026.iloc[-4:])
iloc[-4:] - 最后4个月:
9月 165.7
10月 158.3
11月 172.5
12月 188.6
Name: 2026年月度销售额(万元), dtype: float64
python
print("使用 iloc 列表索引:")
print(" iloc[[0, 2, 4, 6, 8, 10]] - 偶数月份(1,3,5,7,9,11月):")
print(sales_2026.iloc[[0, 2, 4, 6, 8, 10]])
使用 iloc 列表索引:
iloc[[0, 2, 4, 6, 8, 10]] - 偶数月份(1,3,5,7,9,11月):
1月 120.5
3月 142.3
5月 155.2
7月 175.4
9月 165.7
11月 172.5
Name: 2026年月度销售额(万元), dtype: float64
示例3:标签索引 loc - 按标签访问
loc(label location)是基于标签的索引方式:
- 使用索引标签值进行访问
- 切片时左闭右闭
[start, end](包含结束标签) - 当索引是字符串时,必须使用 loc
python
print("使用 loc 按标签访问数据:")
print(f" loc['1月'] - 1月销售额: {sales_2026.loc['1月']}")
print(f" loc['6月'] - 6月销售额: {sales_2026.loc['6月']}")
print(f" loc['12月'] - 12月销售额: {sales_2026.loc['12月']}")
使用 loc 按标签访问数据:
loc['1月'] - 1月销售额: 120.5
loc['6月'] - 6月销售额: 168.9
loc['12月'] - 12月销售额: 188.6
python
print("使用 loc 切片(左闭右闭):")
print(" loc['1月':'3月'] - 第一季度(包含3月):")
print(sales_2026.loc["1月":"3月"])
使用 loc 切片(左闭右闭):
loc['1月':'3月'] - 第一季度(包含3月):
1月 120.5
2月 135.8
3月 142.3
Name: 2026年月度销售额(万元), dtype: float64
python
print(" loc['7月':'9月'] - 第三季度:")
print(sales_2026.loc["7月":"9月"])
loc['7月':'9月'] - 第三季度:
7月 175.4
8月 182.1
9月 165.7
Name: 2026年月度销售额(万元), dtype: float64
python
print("使用 loc 列表索引:")
print(" loc[['1月', '6月', '12月']] - 特定月份:")
print(sales_2026.loc[["1月", "6月", "12月"]])
使用 loc 列表索引:
loc[['1月', '6月', '12月']] - 特定月份:
1月 120.5
6月 168.9
12月 188.6
Name: 2026年月度销售额(万元), dtype: float64
示例4:直接索引 [] - 混合行为(不推荐)
直接使用 [] 索引的行为取决于索引类型:
- 如果索引是字符串:
s['label']按标签访问 - 如果索引是整数:
s[0]优先按标签访问,而非位置 - 切片
s[0:3]总是按位置切片(左闭右开)
⚠️ 为避免混淆,生产代码中建议始终使用 .loc 和 .iloc
python
print("直接索引的行为:")
print(f" sales_2026['1月'](字符串索引): {sales_2026['1月']}")
print(f" sales_2026[0:3](切片,按位置):")
print(sales_2026[0:3])
直接索引的行为:
sales_2026['1月'](字符串索引): 120.5
sales_2026[0:3](切片,按位置):
1月 120.5
2月 135.8
3月 142.3
Name: 2026年月度销售额(万元), dtype: float64
python
# 创建整数索引的 Series 展示潜在问题
int_index_series = pd.Series([10, 20, 30, 40, 50], index=[2, 4, 6, 8, 10])
print("整数索引的潜在混淆(index=[2,4,6,8,10]):")
print(int_index_series)
print(f" s[2] 按标签访问(不是第3个元素): {int_index_series[2]}") # 返回 30
print(f" s[0:3] 按位置切片: {list(int_index_series[0:3])}") # 返回前3个
整数索引的潜在混淆(index=[2,4,6,8,10]):
2 10
4 20
6 30
8 40
10 50
dtype: int64
s[2] 按标签访问(不是第3个元素): 10
s[0:3] 按位置切片: [10, 20, 30]
2.2 Series 数据修改
示例5:修改单个值
python
# 创建可修改的副本
sales_edit = sales_2026.copy()
print("修改单个值:")
print(f" 修改前 1月销售额: {sales_edit.loc['1月']}")
sales_edit.loc["1月"] = 125.0 # 使用 loc 修改
print(f" 修改后 1月销售额: {sales_edit.loc['1月']}")
修改单个值:
修改前 1月销售额: 120.5
修改后 1月销售额: 125.0
示例6:批量修改值
python
sales_edit2 = sales_2026.copy()
print("批量修改值(第一季度上调 10%):")
print(" 修改前第一季度:")
print(sales_edit2.loc["1月":"3月"])
# 使用 loc 批量修改
sales_edit2.loc["1月":"3月"] = sales_edit2.loc["1月":"3月"] * 1.1
print("\n 修改后第一季度(上调10%):")
print(sales_edit2.loc["1月":"3月"])
批量修改值(第一季度上调 10%):
修改前第一季度:
1月 120.5
2月 135.8
3月 142.3
Name: 2026年月度销售额(万元), dtype: float64
修改后第一季度(上调10%):
1月 132.55
2月 149.38
3月 156.53
Name: 2026年月度销售额(万元), dtype: float64
示例7:添加新元素
python
sales_add = sales_2026.copy()
print("添加新元素(全年总计):")
sales_add.loc["全年总计"] = sales_add.sum()
print(sales_add.tail(3))
添加新元素(全年总计):
11月 172.5
12月 188.6
全年总计 1903.9
Name: 2026年月度销售额(万元), dtype: float64
示例8:删除元素
python
print("删除元素:")
sales_del = sales_2026.drop("12月") # 返回新 Series,原 Series 不变
print(f" 原 Series 长度: {len(sales_2026)}")
print(f" 删除后长度: {len(sales_del)}")
删除元素:
原 Series 长度: 12
删除后长度: 11
python
sales_del2 = sales_2026.copy()
sales_del2 = sales_del2.drop(["11月", "12月"])
print(" 删除11-12月后:")
print(sales_del2.tail(3))
删除11-12月后:
8月 182.1
9月 165.7
10月 158.3
Name: 2026年月度销售额(万元), dtype: float64
2.3 数据对齐机制
数据对齐是 Pandas 最强大的特性之一。当对两个 Series 进行运算时,Pandas 会自动根据索引标签对齐数据,而不是简单地按位置对齐。
对齐规则
- 相同索引标签的数据进行运算
- 只存在于一个 Series 中的索引,结果标记为 NaN(缺失值)
- 运算结果包含所有涉及的索引标签
Series A
index-a,b,c
values-1,2,3
Series B
index-b,c,d
values-10,20,30
运算结果
index-a,b,c,d
values-NaN,12,23,NaN
示例9:相同索引的运算
python
# 2026年各月销售额
sales_q1 = pd.Series([120.5, 135.8, 142.3], index=["1月", "2月", "3月"], name="Q1销售额")
# 2025年同期销售额
sales_q1_2025 = pd.Series([110.2, 125.5, 130.8], index=["1月", "2月", "3月"], name="Q1_2025销售额")
print("相同索引的 Series 运算(计算同比增长):")
print(" 2026年Q1销售额:")
print(sales_q1)
print("\n 2025年Q1销售额:")
print(sales_q1_2025)
# 计算同比增长率
growth_rate = ((sales_q1 - sales_q1_2025) / sales_q1_2025 * 100).round(2)
growth_rate.name = "同比增长率(%)"
print("\n 同比增长率(%):")
print(growth_rate)
相同索引的 Series 运算(计算同比增长):
2026年Q1销售额:
1月 120.5
2月 135.8
3月 142.3
Name: Q1销售额, dtype: float64
2025年Q1销售额:
1月 110.2
2月 125.5
3月 130.8
Name: Q1_2025销售额, dtype: float64
同比增长率(%):
1月 9.35
2月 8.21
3月 8.79
Name: 同比增长率(%), dtype: float64
示例10:不同索引的运算 - 自动对齐
python
# 上半年数据
sales_h1 = pd.Series([120.5, 135.8, 142.3, 138.6, 155.2, 168.9],
index=["1月", "2月", "3月", "4月", "5月", "6月"])
# 下半年数据
sales_h2 = pd.Series([175.4, 182.1, 165.7, 158.3, 172.5, 188.6],
index=["7月", "8月", "9月", "10月", "11月", "12月"])
print("不同索引的 Series 相加(全年汇总):")
print(" 上半年销售额:")
print(sales_h1.head(3))
print(" ...")
print("\n 下半年销售额:")
print(sales_h2.head(3))
print(" ...")
# 合并全年数据
full_year = pd.concat([sales_h1, sales_h2])
full_year.name = "2026年全年销售额"
print(f"\n 合并后的全年数据(共{len(full_year)}个月):")
print(full_year.head(4))
print(" ...")
print(full_year.tail(3))
不同索引的 Series 相加(全年汇总):
上半年销售额:
1月 120.5
2月 135.8
3月 142.3
dtype: float64
...
下半年销售额:
7月 175.4
8月 182.1
9月 165.7
dtype: float64
...
合并后的全年数据(共12个月):
1月 120.5
2月 135.8
3月 142.3
4月 138.6
Name: 2026年全年销售额, dtype: float64
...
10月 158.3
11月 172.5
12月 188.6
Name: 2026年全年销售额, dtype: float64
示例11:部分重叠索引的运算
python
# 某产品线销售额
product_a = pd.Series([50, 55, 60], index=["1月", "2月", "3月"], name="产品A")
# 另一产品线销售额(有重叠月份)
product_b = pd.Series([30, 35, 40, 45], index=["2月", "3月", "4月", "5月"], name="产品B")
print("部分重叠索引的运算:")
print(" 产品A销售额:")
print(product_a)
print("\n 产品B销售额:")
print(product_b)
total_sales = product_a + product_b
print("\n 两产品销售额相加(注意1月和4-5月产生NaN):")
print(total_sales)
部分重叠索引的运算:
产品A销售额:
1月 50
2月 55
3月 60
Name: 产品A, dtype: int64
产品B销售额:
2月 30
3月 35
4月 40
5月 45
Name: 产品B, dtype: int64
两产品销售额相加(注意1月和4-5月产生NaN):
1月 NaN
2月 85.0
3月 95.0
4月 NaN
5月 NaN
dtype: float64
2.4 缺失值处理
在数据分析中,缺失值是常见问题。Pandas 使用 NaN(Not a Number)表示缺失值,它是 float 类型的特殊值。
缺失值处理流程
- 检测缺失值
- 分析缺失原因
- 选择处理策略(删除/填充)
- 验证处理结果
python
# 创建包含缺失值的 Series(模拟某产品部分月份数据缺失)
product_sales = pd.Series(
[45.2, np.nan, 52.8, 48.5, np.nan, 61.3, 58.9, np.nan, 65.2, 62.1, 68.5, 72.3],
index=["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
name="产品X月度销售额(万元)"
)
示例12:检测缺失值
python
print("原始数据(含缺失值):")
print(product_sales)
原始数据(含缺失值):
1月 45.2
2月 NaN
3月 52.8
4月 48.5
5月 NaN
6月 61.3
7月 58.9
8月 NaN
9月 65.2
10月 62.1
11月 68.5
12月 72.3
Name: 产品X月度销售额(万元), dtype: float64
python
print("\n缺失值检测方法:")
print(f" isnull() - 判断是否为缺失值:")
print(product_sales.isnull())
缺失值检测方法:
isnull() - 判断是否为缺失值:
1月 False
2月 True
3月 False
4月 False
5月 True
6月 False
7月 False
8月 True
9月 False
10月 False
11月 False
12月 False
Name: 产品X月度销售额(万元), dtype: bool
python
print(f"\n notnull() - 判断是否非缺失值:")
print(product_sales.notnull())
notnull() - 判断是否非缺失值:
1月 True
2月 False
3月 True
4月 True
5月 False
6月 True
7月 True
8月 False
9月 True
10月 True
11月 True
12月 True
Name: 产品X月度销售额(万元), dtype: bool
python
print(f"\n 缺失值统计:")
print(f" 缺失值数量: {product_sales.isnull().sum()}")
print(f" 非缺失值数量: {product_sales.notnull().sum()}")
print(f" 缺失比例: {product_sales.isnull().mean() * 100:.1f}%")
缺失值统计:
缺失值数量: 3
非缺失值数量: 9
缺失比例: 25.0%
示例13:删除缺失值
python
print("删除缺失值:")
print(" dropna() - 删除含缺失值的行:")
cleaned = product_sales.dropna()
print(cleaned)
print(f" 删除后剩余 {len(cleaned)} 条数据")
删除缺失值:
dropna() - 删除含缺失值的行:
1月 45.2
3月 52.8
4月 48.5
6月 61.3
7月 58.9
9月 65.2
10月 62.1
11月 68.5
12月 72.3
Name: 产品X月度销售额(万元), dtype: float64
删除后剩余 9 条数据
示例14:填充缺失值
python
print("使用固定值填充:")
filled_zero = product_sales.fillna(0)
print(" fillna(0) - 用0填充:")
print(filled_zero)
使用固定值填充:
fillna(0) - 用0填充:
1月 45.2
2月 0.0
3月 52.8
4月 48.5
5月 0.0
6月 61.3
7月 58.9
8月 0.0
9月 65.2
10月 62.1
11月 68.5
12月 72.3
Name: 产品X月度销售额(万元), dtype: float64
python
print("\n使用统计值填充:")
mean_value = product_sales.mean()
print(f" 平均值: {mean_value:.2f}")
filled_mean = product_sales.fillna(mean_value)
print(" fillna(mean) - 用平均值填充:")
print(filled_mean)
使用统计值填充:
平均值: 59.42
fillna(mean) - 用平均值填充:
1月 45.200000
2月 59.422222
3月 52.800000
4月 48.500000
5月 59.422222
6月 61.300000
7月 58.900000
8月 59.422222
9月 65.200000
10月 62.100000
11月 68.500000
12月 72.300000
Name: 产品X月度销售额(万元), dtype: float64
python
print("\n使用前向/后向填充(适合时间序列):")
print(" ffill() - 前向填充(用前一个有效值):")
filled_ffill = product_sales.ffill()
print(filled_ffill)
使用前向/后向填充(适合时间序列):
ffill() - 前向填充(用前一个有效值):
1月 45.2
2月 45.2
3月 52.8
4月 48.5
5月 48.5
6月 61.3
7月 58.9
8月 58.9
9月 65.2
10月 62.1
11月 68.5
12月 72.3
Name: 产品X月度销售额(万元), dtype: float64
python
print("\n bfill() - 后向填充(用后一个有效值):")
filled_bfill = product_sales.bfill()
print(filled_bfill)
bfill() - 后向填充(用后一个有效值):
1月 45.2
2月 52.8
3月 52.8
4月 48.5
5月 61.3
6月 61.3
7月 58.9
8月 65.2
9月 65.2
10月 62.1
11月 68.5
12月 72.3
Name: 产品X月度销售额(万元), dtype: float64
python
print("\n使用线性插值:")
print(" interpolate() - 线性插值:")
filled_interp = product_sales.interpolate(method="linear")
print(filled_interp)
使用线性插值:
interpolate() - 线性插值:
1月 45.20
2月 49.00
3月 52.80
4月 48.50
5月 54.90
6月 61.30
7月 58.90
8月 62.05
9月 65.20
10月 62.10
11月 68.50
12月 72.30
Name: 产品X月度销售额(万元), dtype: float64
2.5 向量化运算与统计
向量化运算是 Pandas 的核心优势,它允许对整个 Series 进行批量操作,无需编写显式循环,代码更简洁且执行效率更高。
NumPy 底层优化
Pandas 的向量化运算基于 NumPy 实现,底层使用 C 语言优化,比纯 Python 循环快数十倍甚至上百倍。
示例15:基本数学运算
python
# 使用2026年完整销售数据
sales = sales_2026.copy()
print("基本数学运算(向量化):")
print(" 原始销售额(万元):")
print(sales.head())
基本数学运算(向量化):
原始销售额(万元):
1月 120.5
2月 135.8
3月 142.3
4月 138.6
5月 155.2
Name: 2026年月度销售额(万元), dtype: float64
python
print("\n sales * 10000 - 转换为元:")
print((sales * 10000).head())
sales * 10000 - 转换为元:
1月 1205000.0
2月 1358000.0
3月 1423000.0
4月 1386000.0
5月 1552000.0
Name: 2026年月度销售额(万元), dtype: float64
python
print("\n sales + 10 - 每月增加10万元:")
print((sales + 10).head())
sales + 10 - 每月增加10万元:
1月 130.5
2月 145.8
3月 152.3
4月 148.6
5月 165.2
Name: 2026年月度销售额(万元), dtype: float64
python
print("\n sales / 30 - 日均销售额(假设每月30天):")
daily_avg = (sales / 30).round(2)
print(daily_avg.head())
sales / 30 - 日均销售额(假设每月30天):
1月 4.02
2月 4.53
3月 4.74
4月 4.62
5月 5.17
Name: 2026年月度销售额(万元), dtype: float64
示例16:常用统计方法
python
print("描述性统计:")
print(f" count() - 非缺失值数量: {sales.count()}")
print(f" sum() - 总和: {sales.sum():.2f} 万元")
print(f" mean() - 平均值: {sales.mean():.2f} 万元")
print(f" median() - 中位数: {sales.median():.2f} 万元")
print(f" std() - 标准差: {sales.std():.2f} 万元")
print(f" min() - 最小值: {sales.min():.2f} 万元")
print(f" max() - 最大值: {sales.max():.2f} 万元")
描述性统计:
count() - 非缺失值数量: 12
sum() - 总和: 1903.90 万元
mean() - 平均值: 158.66 万元
median() - 中位数: 162.00 万元
std() - 标准差: 20.74 万元
min() - 最小值: 120.50 万元
max() - 最大值: 188.60 万元
python
print("\ndescribe() - 综合统计:")
print(sales.describe())
describe() - 综合统计:
count 12.000000
mean 158.658333
std 20.742905
min 120.500000
25% 141.375000
50% 162.000000
75% 173.225000
max 188.600000
Name: 2026年月度销售额(万元), dtype: float64
示例17:累积运算
python
print("累积运算:")
print(" cumsum() - 累积和(计算累计销售额):")
cumulative = sales.cumsum()
print(cumulative.head(6))
累积运算:
cumsum() - 累积和(计算累计销售额):
1月 120.5
2月 256.3
3月 398.6
4月 537.2
5月 692.4
6月 861.3
Name: 2026年月度销售额(万元), dtype: float64
python
print("\n cummax() / cummin() - 累积最大/最小值:")
print(" 截至当月的最高销售额记录:")
print(sales.cummax().head(6))
cummax() / cummin() - 累积最大/最小值:
截至当月的最高销售额记录:
1月 120.5
2月 135.8
3月 142.3
4月 142.3
5月 155.2
6月 168.9
Name: 2026年月度销售额(万元), dtype: float64
示例18:条件统计
python
print("条件统计:")
print(f" 销售额超过150万元的月份数量: {(sales > 150).sum()}")
print(f" 销售额超过150万元的月份: {list(sales[sales > 150].index)}")
条件统计:
销售额超过150万元的月份数量: 8
销售额超过150万元的月份: ['5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
本章小结
学习内容回顾
-
索引机制
iloc: 位置索引(从0开始,左闭右开)loc: 标签索引(左闭右闭)- 推荐始终使用 iloc/loc 明确索引类型
-
数据修改
- 修改单个值:
s.loc['label'] = value - 批量修改:
s.loc[start:end] = values - 添加元素:
s.loc['new_label'] = value - 删除元素:
s.drop('label')
- 修改单个值:
-
数据对齐
- Series 运算按索引标签自动对齐
- 不匹配的索引产生 NaN
- 这是 Pandas 最强大的特性之一
-
缺失值处理
- 检测:
isnull(),notnull() - 删除:
dropna() - 填充:
fillna(),ffill(),bfill(),interpolate()
- 检测:
-
向量化运算
- 支持
+,-,*,/等数学运算 - 统计方法:
sum(),mean(),std(),describe() - 累积运算:
cumsum(),cummax(),cummin()
- 支持
下章预告
第三章:DataFrame 对象详解
将深入学习 DataFrame 的高级用法,包括:
- 行列操作(增删改查)
- 数据选择(loc/iloc高级用法)
- 索引设置与重置
- 数据筛选与过滤
课后练习
请尝试完成以下练习:
练习1:创建两个 Series,分别存储 2026 年上半年和下半年的销售额,然后计算全年总销售额(注意处理索引对齐)。
练习2:给定一个包含缺失值的 Series,分别使用以下方法处理缺失值,并比较结果差异:
- 用平均值填充
- 用中位数填充
- 前向填充
- 线性插值
练习3:创建一个表示班级学生成绩的 Series,然后:
- 计算平均分、最高分和最低分
- 找出成绩大于等于 90 分的学生
- 对成绩进行排序
- 将成绩分为 A(>=90)、B(>=80)、C(<80) 三个等级
练习4:基于实战案例的数据,计算:
- 每个月的环比增长率
- 销售额与订单量的相关系数
- 找出客单价最高的月份