You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从DataFrame计算温度事件相对频率:八月≥20℃、一月≤2℃等场景

四类温度事件相对频率计算方案

核心逻辑

Pandas 的布尔序列在数值运算中,True 等价于1,False 等价于0,对布尔序列取 .mean() 即可直接得到满足条件的样本占总样本的比例(即相对频率),乘以100即可得到百分比形式的频率。

实现代码

import pandas as pd

# 1. 定义四类场景的布尔条件
cond_aug_ge20 = df_temp1['aug'] >= 20
cond_jan_le2 = df_temp1['jan'] <= 2
cond_both = cond_aug_ge20 & cond_jan_le2
cond_either = cond_aug_ge20 | cond_jan_le2

# 2. 计算相对频率(0~1区间)
freq_relative = pd.Series({
    "8月气温≥20℃": cond_aug_ge20.mean(),
    "1月气温≤2℃": cond_jan_le2.mean(),
    "同时满足8月≥20℃且1月≤2℃": cond_both.mean(),
    "满足8月≥20℃或1月≤2℃": cond_either.mean()
})

# 3. 转换为百分比频率
freq_percent = freq_relative * 100

# 打印结果查看
print(freq_percent)

如果需要统计满足条件的样本绝对数量,将 .mean() 替换为 .sum() 即可。


内容的提问来源于stack exchange,提问作者mm_geog_24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 08:15:07