三、pandas-Series-案例

suaxi
2025-11-23 / 0 评论 / 3 阅读 / 正在检测是否收录...
(1)学生成绩统计

创建一个包含10名学生成绩的Series,成绩范围在50 - 100之间。
计算平均分、最高分、最低分,并找出高于平均分的学生人数。

import numpy as np
import pandas as pd

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

学生1     87
学生2     93
学生3     62
学生4     58
学生5     59
学生6     61
学生7     55
学生8     65
学生9     50
学生10    66
dtype: int32
mean_score = scores.mean()
print("平均分:", mean_score)

print("最高分:", scores.max())

print("最低分:", scores.min())

print("成绩高于平均分的学生人数:", scores[scores > mean_score].count())

平均分: 65.6
最高分: 93
最低分: 50
成绩高于平均分的学生人数: 3


(2)温度数据分析

给定某城市一周中每天的最高温度Series,计算温度超过30°的天数,平均温度,温度由高到低排序,并找出温度变化最大的两天

import numpy as np
import pandas as pd

# 温度
temperature = pd.Series([28, 26, 33, 31, 32, 30, 27], index=['周一', '周二', '周三', '周四', '周五', '周六', '周日', ])
print(temperature)

周一    28
周二    26
周三    33
周四    31
周五    32
周六    30
周日    27
dtype: int64
print("温度超过30°的天数:", temperature[temperature > 30].count())
print("平均温度:", round(temperature.mean(), 1))
# ascending=False 降序
temperature_sort = temperature.sort_values(ascending=False)
print(temperature_sort)

# 计算相邻两天的温度差值
temperature_diff = temperature.diff().abs()
temperature_diff_sort = temperature_diff.sort_values(ascending=False)
print("温度变化最大的两天:", temperature_diff_sort.index[:2].tolist())

温度超过30°的天数: 3
平均温度: 29.6
周三    33
周五    32
周四    31
周六    30
周一    28
周日    27
周二    26
dtype: int64
温度变化最大的两天: ['周三', '周日']


(3)股票价格分析

给定某股票连续10个交易日的收盘价Series,计算每日收益率(当日收盘价/前日收盘价 - 1),找出收益率最高和最低的日期,计算波动率(收益率标准差)

import numpy as np
import pandas as pd

prices = pd.Series([105.2, 103.1, 104.6, 102.9, 101.9, 106, 108.2, 105.1, 104.6, 102.9], index=pd.date_range('2025-10-01', periods=10))
print(prices)

2025-10-01    105.2
2025-10-02    103.1
2025-10-03    104.6
2025-10-04    102.9
2025-10-05    101.9
2025-10-06    106.0
2025-10-07    108.2
2025-10-08    105.1
2025-10-09    104.6
2025-10-10    102.9
Freq: D, dtype: float64
print("收益率:")
pct_change = prices.pct_change()
print(pct_change)

print("收益率最高的日期:", pct_change.idxmax())
print("收益率最低的日期:", pct_change.idxmin())

print("波动率:", pct_change.std())

收益率:
2025-10-01         NaN
2025-10-02   -0.019962
2025-10-03    0.014549
2025-10-04   -0.016252
2025-10-05   -0.009718
2025-10-06    0.040236
2025-10-07    0.020755
2025-10-08   -0.028651
2025-10-09   -0.004757
2025-10-10   -0.016252
Freq: D, dtype: float64
收益率最高的日期: 2025-10-06 00:00:00
收益率最低的日期: 2025-10-08 00:00:00
波动率: 0.022586890620196715


(4)销售数据分析

某产品过去12个月的销售量Series,计算季度平均销量,销量最高的月份,月环比增长率,连续增长超过两个月的月份

import numpy as np
import pandas as pd

sales = pd.Series([130, 135, 136, 131, 128, 130, 135, 137, 141, 135, 136, 133],
                  index=pd.date_range('2024-02-01', periods=12, freq='ME'))
print(sales)

2024-02-29    130
2024-03-31    135
2024-04-30    136
2024-05-31    131
2024-06-30    128
2024-07-31    130
2024-08-31    135
2024-09-30    137
2024-10-31    141
2024-11-30    135
2024-12-31    136
2025-01-31    133
Freq: ME, dtype: int64
# resample() 重新采样
print("季度平均销量:")
print(sales.resample("QE").mean())

季度平均销量:
2024-03-31    132.500000
2024-06-30    131.666667
2024-09-30    134.000000
2024-12-31    137.333333
2025-03-31    133.000000
Freq: QE-DEC, dtype: float64
print("销量最高的月份:", sales.idxmax())

销量最高的月份: 2024-10-31 00:00:00
print("月环比增长率:")
sales_pct_change = sales.pct_change()
print(sales_pct_change)

月环比增长率:
2024-02-29         NaN
2024-03-31    0.038462
2024-04-30    0.007407
2024-05-31   -0.036765
2024-06-30   -0.022901
2024-07-31    0.015625
2024-08-31    0.038462
2024-09-30    0.014815
2024-10-31    0.029197
2024-11-30   -0.042553
2024-12-31    0.007407
2025-01-31   -0.022059
Freq: ME, dtype: float64
print("连续增长超过两个月的月份:")
sales_up = sales_pct_change > 0

# rolling() 滑动窗口
rolling_up = sales_up.rolling(3).sum() == 3
print(sales_up[rolling_up].keys().tolist())

连续增长超过两个月的月份:
[Timestamp('2024-09-30 00:00:00'), Timestamp('2024-10-31 00:00:00')]


(5)每小时销售数据分析

现有某商店每小时销售额Series,按天重采样计算每日的销售总额,计算每天营业时间(8:00 - 22:00)和非营业时间的销售额比例,找出销售额最高的三个小时

import numpy as np
import pandas as pd

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

2025-11-01 00:00:00    37
2025-11-01 01:00:00    12
2025-11-01 02:00:00    72
2025-11-01 03:00:00     9
2025-11-01 04:00:00    75
2025-11-01 05:00:00     5
2025-11-01 06:00:00    79
2025-11-01 07:00:00    64
2025-11-01 08:00:00    16
2025-11-01 09:00:00     1
2025-11-01 10:00:00    76
2025-11-01 11:00:00    71
2025-11-01 12:00:00     6
2025-11-01 13:00:00    25
2025-11-01 14:00:00    50
2025-11-01 15:00:00    20
2025-11-01 16:00:00    18
2025-11-01 17:00:00    84
2025-11-01 18:00:00    11
2025-11-01 19:00:00    28
2025-11-01 20:00:00    29
2025-11-01 21:00:00    14
2025-11-01 22:00:00    50
2025-11-01 23:00:00    68
Freq: h, dtype: int32
# 按天重采样
day_sales = hour_sales.resample('D').sum()
print("当天销售总额:", day_sales)
# print("当天销售总额:", hour_sales.sum())

当天销售总额: 2025-11-01    920
Freq: D, dtype: int32
# 营业时间的销售额
# business_hour_sales = hour_sales.between_time('8:00', '22:00')
business_mask = (hour_sales.index.hour>=8) & (hour_sales.index.hour<=22)
business_hour_sales = hour_sales[business_mask]
business_sales = business_hour_sales.sum()

# 非营业时间的销售额
# not_business_sales = day_sales - business_sales
not_business_sales = hour_sales.drop(business_hour_sales.index).sum()
print("营业时间与非营业时间销售额比例:")
print(business_sales / not_business_sales)

营业时间与非营业时间销售额比例:
1.185273159144893
print("销售额最高的三个小时:")
#print(hour_sales.sort_values().tail(3))
print(hour_sales.nlargest(3))

销售额最高的三个小时:
2025-11-01 17:00:00    84
2025-11-01 06:00:00    79
2025-11-01 10:00:00    76
dtype: int32
0

评论 (0)

取消