numpy 将基于列的值与基于条件的列相乘,python

e0bqpujr  于 2022-11-10  发布在  Python
关注(0)|答案(2)|浏览(91)

我需要编写一个函数,允许我将时间序列数据(以风速为参数)转换为涡轮机的功率输出。输出基于几个间隔。
因此,我尝试创建一个额外的 Dataframe 来存储转换系数。
问题是,我总是得到错误‘浮动’对象是不可订阅的。我知道这一定与浮点数不可订阅或不能在索引处访问有关,但我想不出如何更改参数。

import pandas as pd
time = pd.date_range(start='2019-01-06 20:00:00', end='2019-01-07 03:00:00', freq='H')

df_ws = pd.DataFrame({"wind_speed_hh": 
                   [3.359367, 2.695838, 3.036351, 6.64743,
                    9.93, 13.13, 15.574893, 17.3432]}, index = time)

df_ws['power_production_Vestas'] = 0.0

Weight_Vestas是换算系数

weight_vestas = pd.DataFrame(
    {"wv1": [0.0],
     "wv2": [0.2],
     "wv3": [0.5],
     "wv4": [1.4],
     "wv5": [2.6],
     "wv6": [3.0],
     "wv7": [3.0],
     "wv8": [3.0]})
weight_vestas.head()

其功能是:

def conv_test(x):
    if 4 <= x['wind_speed_hh']:
        return (weight_vestas['wv1'] * x['wind_speed_hh'])
    elif 4 < x['df_ws.wind_speed_hh'] < 6:
        return (weight_vestas['wv2'] * x['df_ws.wind_speed_hh'])
    elif 6 <= x['df_ws.wind_speed_hh'] < 8:
        return (weight_vestas['wv3'] * x['df_ws.wind_speed_hh'])
    elif 8 <= x['df_ws.wind_speed_hh'] < 10:
        return (weight_vestas['wv4'] * x['df_ws.wind_speed_hh'])
    elif 10 <= x['df_ws.wind_speed_hh'] < 12:
        return (weight_vestas['wv5'] * x['df_ws.wind_speed_hh'])
    elif 12 <= x['df_ws.wind_speed_hh'] < 14:
        return (weight_vestas['wv6'] * x['df_ws.wind_speed_hh'])
    elif 14 <= x['df_ws.wind_speed_hh'] < 16:
        return (weight_vestas['wv7'] * x['df_ws.wind_speed_hh'])
    elif 16 <= x['df_ws.wind_speed_hh']:
        return (weight_vestas['wv8'] * x['df_ws.wind_speed_hh'])
    return ''

运行函数并添加生产输出:

df_ws = df_ws.power_production_Vestas.apply(conv_test)
df_test.head()
plupiseo

plupiseo1#

您在power_production_Vestas列中使用了.apply方法,这意味着该函数将应用于该列的每个元素。这就是为什么会出现“浮点型不可订阅”错误,conv_testx参数是浮点型。
尝试使用相同的方法,但在 Dataframe 上,x将是一个系列。

df_ws.apply(conv_test, axis=1)

axis=1确保您将沿着行而不是列前进。

xnifntxz

xnifntxz2#

使用pd.cut代替conv_test

s1 = pd.cut(
    df_ws['wind_speed_hh'], 
    bins=[0, 4, 6, 8, 10, 12], 
    labels=[0.00, 0.20, 0.50, 1.40, 2.60], 
    right=False).astype('float').fillna(3.00)
s1 * df_ws['wind_speed_hh']

输出:

2019-01-06 20:00:00    0.00
2019-01-06 21:00:00    0.00
2019-01-06 22:00:00    0.00
2019-01-06 23:00:00    3.32
2019-01-07 00:00:00   13.90
2019-01-07 01:00:00   39.39
2019-01-07 02:00:00   46.72
2019-01-07 03:00:00   52.03
Freq: H, Name: wind_speed_hh, dtype: float64

相关问题