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

如何基于Wind列分位数生成电价基准动态调整的DataFrame新列?

风能发电量联动电价调整方案

问题描述

需基于风能发电量(wind列)调整基准电价(Cost列),规则如下:

  • 当wind处于最小值-25%分位数区间:Cost增加5
  • 当wind处于25%-50%分位数区间:Cost增加2.5
  • 当wind处于50%-75%分位数区间:Cost减少2.5
  • 当wind处于75%-最大值区间:Cost减少5

现有数据集示例:

import pandas as pd

# 示例数据
data = {
    'wind': [4281.0, 4384.083333, 4405.666667, 4514.666667, 4692.416667, 9698.0, 9854.083333, 9880.916667, 10356.5, 10478.5],
    'Cost': [12.7225, 11.34, 11.34, 9.933, 9.492, 32.8755, 34.38225, 29.61, 11.76, 15.75],
    'datetime': pd.date_range(start='2021-01-01 01:00', periods=10, freq='H')
}
df = pd.DataFrame(data).set_index('datetime')[['wind', 'Cost']]

数据集统计信息:

wind           Cost
count 8760.000000   8760.000000
mean 5588.449878    22.131348
std 3774.164710     9.547735
min 56.333333       0.042000
25% 2297.604167     13.475437
50% 4792.375000     20.160000
75% 8710.187500     33.012000
max 14132.166667    34.996500

解决方案

方法一:使用pd.cut实现区间映射

直接基于分位数划分区间,映射调整值后计算新电价:

# 提取wind列的分位数阈值
q_vals = df['wind'].describe()[['min', '25%', '50%', '75%', 'max']].values

# 定义区间对应的调整值
adjustments = [5, 2.5, -2.5, -5]

# 为每行分配调整值
df['adjustment'] = pd.cut(df['wind'], bins=q_vals, labels=adjustments, include_lowest=True)

# 计算调整后的电价
df['adjusted_cost'] = df['Cost'] + df['adjustment'].astype(float)

方法二:使用np.select实现条件判断

通过显式条件列表匹配调整值,逻辑更直观:

import numpy as np

# 定义区间条件
conditions = [
    df['wind'] <= df['wind'].quantile(0.25),
    (df['wind'] > df['wind'].quantile(0.25)) & (df['wind'] <= df['wind'].quantile(0.5)),
    (df['wind'] > df['wind'].quantile(0.5)) & (df['wind'] <= df['wind'].quantile(0.75)),
    df['wind'] > df['wind'].quantile(0.75)
]

# 对应条件的调整值
choices = [5, 2.5, -2.5, -5]

# 直接计算调整后的电价
df['adjusted_cost'] = df['Cost'] + np.select(conditions, choices)

说明

  • pd.cut适合基于分位数的标准化区间划分,无需手动写条件
  • np.select更灵活,可应对复杂的自定义区间规则
  • 两种方法均直接从数据集提取分位数,避免硬编码,适配数据动态变化

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 03:24:21