我需要编写一个函数,允许我将时间序列数据(以风速为参数)转换为涡轮机的功率输出。输出基于几个间隔。
因此,我尝试创建一个额外的 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()
2条答案
按热度按时间plupiseo1#
您在
power_production_Vestas
列中使用了.apply
方法,这意味着该函数将应用于该列的每个元素。这就是为什么会出现“浮点型不可订阅”错误,conv_test
的x
参数是浮点型。尝试使用相同的方法,但在 Dataframe 上,
x
将是一个系列。axis=1
确保您将沿着行而不是列前进。xnifntxz2#
使用
pd.cut
代替conv_test
输出: