mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6mobile wallpaper 7mobile wallpaper 8mobile wallpaper 9mobile wallpaper 10mobile wallpaper 11mobile wallpaper 12mobile wallpaper 13mobile wallpaper 14mobile wallpaper 15mobile wallpaper 16mobile wallpaper 17mobile wallpaper 18mobile wallpaper 19mobile wallpaper 20mobile wallpaper 21mobile wallpaper 22mobile wallpaper 23
779 字
2 分钟
笔记|数据分析学习笔记16|Series案例

本系列所有文章都放在数据科学标签,总目录如下:

合集|数据分析学习笔记|系列总目录

上一篇:笔记|数据分析学习笔记15|Series的常用方法


16.1 案例概述#

本节通过五个实际案例,综合运用 Series 的创建、筛选、统计、排序等方法,巩固前几节所学知识。

使用的库:

import numpy as np
import pandas as pd

16.2 学生成绩统计#

生成 10 名学生的随机成绩,进行基本统计分析。

np.random.seed(42)
scores = pd.Series(np.random.randint(50, 101, 10),
index=['学生' + str(i) for i in range(1, 11)])
print(scores)

输出:

学生1 88
学生2 78
学生3 64
学生4 92
学生5 57
学生6 70
学生7 88
学生8 68
学生9 72
学生10 60
dtype: int32
print('平均分:', scores.mean())
print('最高分:', scores.max())
print('最低分:', scores.min())
print('高于平均分的人数:', scores[scores > scores.mean()].count())

输出:

平均分: 73.7
最高分: 92
最低分: 57
高于平均分的人数: 4

涉及方法: mean()max()min()、布尔索引筛选、count()


16.3 温度数据分析#

分析一周温度数据,找出最高温所在日和温度变化最大的两天。

temperatures = pd.Series([28, 31, 29, 32, 30, 27, 33],
index=['周一', '周二', '周三', '周四', '周五', '周六', '周日'])
# 温度超过30的天数
print('超30℃天数:', temperatures[temperatures > 30].count())
print('平均温度:', temperatures.mean())
# 按温度降序排列
print(temperatures.sort_values(ascending=False))

输出:

超30℃天数: 3
平均温度: 30.0
周日 33
周四 32
周二 31
周五 30
周三 29
周一 28
周六 27
dtype: int64
# 计算温度变化最大的两天
# 原始温度: 28 31 29 32 30 27 33
# 相邻差: nan 3 -2 3 -2 -3 6
t3 = temperatures.diff().abs()
print('温度变化最大的2天:', t3.sort_values(ascending=False).index[:2].tolist())

输出:

温度变化最大的2天: ['周日', '周二']

分析过程:

  1. diff():计算相邻两天的温差。
  2. abs():取绝对值,获取变化幅度(不分正负)。
  3. sort_values(ascending=False):按变化幅度降序排列。
  4. index[:2].tolist():取前两名的索引(周几)并转为列表。

16.4 股票价格分析#

模拟 10 天的股票收盘价,计算每日收益率和波动率。

prices = pd.Series([102.3, 103.5, 105.1, 104.8, 106.2, 107.0,
106.5, 108.1, 109.3, 110.2],
index=pd.date_range('2023-01-01', periods=10))
print(prices)

输出:

2023-01-01 102.3
2023-01-02 103.5
2023-01-03 105.1
2023-01-04 104.8
2023-01-05 106.2
2023-01-06 107.0
2023-01-07 106.5
2023-01-08 108.1
2023-01-09 109.3
2023-01-10 110.2
Freq: D, dtype: float64

pd.date_range() 可以快速生成连续的日期索引,periods 指定生成天数。

# 每日收益率 = 当日收盘价 / 前日收盘价 - 1
a = prices.pct_change()
print(a)

输出:

2023-01-01 NaN
2023-01-02 0.011730
2023-01-03 0.015459
2023-01-04 -0.002854
2023-01-05 0.013359
2023-01-06 0.007533
2023-01-07 -0.004673
2023-01-08 0.015023
2023-01-09 0.011101
2023-01-10 0.008234
Freq: D, dtype: float64

第一天为 NaN,因为没有前一天的数据可供计算。

print('收益率最高的日期:', a.idxmax())
print('收益率最低的日期:', a.idxmin())
print('波动率(标准差):', a.std())

输出:

收益率最高的日期: 2023-01-03 00:00:00
收益率最低的日期: 2023-01-07 00:00:00
波动率(标准差): 0.007373623845361105

涉及方法: pd.date_range()pct_change()idxmax()idxmin()std()


16.5 销售数据分析#

分析 2022 年月度销售数据,实现季度汇总和连续增长检测。

sales = pd.Series([120, 135, 145, 160, 155, 170, 180, 175, 190, 200, 210, 220],
index=pd.date_range('2022-01-01', periods=12, freq='MS'))
print(sales)

输出:

2022-01-01 120
2022-02-01 135
2022-03-01 145
2022-04-01 160
2022-05-01 155
2022-06-01 170
2022-07-01 180
2022-08-01 175
2022-09-01 190
2022-10-01 200
2022-11-01 210
2022-12-01 220
Freq: MS, dtype: int64

freq='MS' 表示月初(Month Start),生成每月第一天。

季度平均销量:

print(sales.resample('QS').mean())

输出:

2022-01-01 133.333333
2022-04-01 161.666667
2022-07-01 181.666667
2022-10-01 210.000000
Freq: QS-JAN, dtype: float64

resample('QS') 按季度(Quarter Start)对时间序列做重采样,配合 mean() 计算每季度平均值。

月环比增长率:

print(sales.pct_change())

输出:

2022-01-01 NaN
2022-02-01 0.125000
2022-03-01 0.074074
2022-04-01 0.103448
2022-05-01 -0.031250
2022-06-01 0.096774
2022-07-01 0.058824
2022-08-01 -0.027778
2022-09-01 0.085714
2022-10-01 0.052632
2022-11-01 0.050000
2022-12-01 0.047619
Freq: MS, dtype: float64

连续增长超过 2 个月的月份(滑动窗口算法):

a = sales.pct_change()
b = a > 0
print(list(b[b.rolling(3).sum() == 3].keys()))

输出:

[Timestamp('2022-04-01 00:00:00'), Timestamp('2022-11-01 00:00:00'), Timestamp('2022-12-01 00:00:00')]

分析过程:

  1. pct_change():计算逐月环比增长率。
  2. > 0:生成布尔 Series,标记增长(True)或下降(False)。
  3. rolling(3).sum():滑动窗口大小为 3,计算连续 3 个月中增长的月份数。
  4. == 3:筛选出连续 3 个月都增长的月份。

涉及方法: resample()pct_change()idxmax()rolling()keys()


16.6 每小时销售数据分析#

生成一天 24 小时的随机销售数据,按天聚合计算总销售额。

np.random.seed(42)
hourly_sales = pd.Series(np.random.randint(1, 100, 24),
index=pd.date_range('2025-01-01', periods=24, freq='h'))
print(hourly_sales)

输出:

2025-01-01 00:00:00 52
2025-01-01 01:00:00 93
2025-01-01 02:00:00 15
2025-01-01 03:00:00 72
2025-01-01 04:00:00 61
2025-01-01 05:00:00 21
2025-01-01 06:00:00 83
2025-01-01 07:00:00 87
2025-01-01 08:00:00 75
2025-01-01 09:00:00 75
2025-01-01 10:00:00 88
2025-01-01 11:00:00 24
2025-01-01 12:00:00 3
2025-01-01 13:00:00 22
2025-01-01 14:00:00 53
2025-01-01 15:00:00 2
2025-01-01 16:00:00 88
2025-01-01 17:00:00 30
2025-01-01 18:00:00 38
2025-01-01 19:00:00 2
2025-01-01 20:00:00 64
2025-01-01 21:00:00 60
2025-01-01 22:00:00 21
2025-01-01 23:00:00 33
Freq: h, dtype: int32

freq='h' 表示按小时生成时间序列。

按天汇总总销售额:

print(hourly_sales.resample('D').sum())

输出:

2025-01-01 1162
Freq: D, dtype: int32

resample('D') 将 24 条小时数据聚合为 1 条日数据,配合 sum() 计算当天总销售额。


16.7 本节小结#

本节通过五个案例综合运用了 Series 的核心方法:

案例涉及方法
学生成绩统计mean()max()min()、布尔索引、count()
温度数据分析sort_values()diff()abs()tolist()
股票价格分析pd.date_range()pct_change()idxmax()idxmin()std()
销售数据分析resample()pct_change()rolling()keys()
每小时销售分析pd.date_range(freq='h')resample('D').sum()

下一节我们将学习 DataFrame 的介绍和创建

下一篇:笔记|数据分析学习笔记17|DataFrame的介绍和创建


分享

如果这篇文章对你有帮助,欢迎分享给更多人!

笔记|数据分析学习笔记16|Series案例
https://luq-blog.xyz/posts/2026-07-03-data-notes-16-series-cases/
作者
Luquiescene
发布于
2026-07-03
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录