以下是一个更大的数据集的示例:
df_old = pd.DataFrame({'code': ['fea-1','fea-132','fea-223','fea-394','fea-595','fea-130','fea-495'],
'forecastWind_low':[20,15,0,45,45,25,45],
'forecastWind_high':['NaN' ,30,'NaN',55,65,35,'NaN'],
'obs_windSpeed':[20,11,3,65,55,'NaN',55]})我预测了风速,我需要与观测值进行比较。最终,我需要找到与观测风速值最接近的预测风速(低或高),以获得如下输出:
df_new = pd.DataFrame({'code': ['fea-1','fea-132','fea-223','fea-394','fea-595','fea-130','fea-495'],
'forecastWind_low':[20,15,0,45,45,25,45],
'forecastWind_high':['NaN' ,30,'NaN',55,65,35,'NaN'],
'obs_windSpeed':[20,11,3,65,55,'NaN',55],
'nearest_forecast_windSpeed':[20,15,0,55,45,'NaN',45]})发布于 2020-09-04 09:22:40
创建自定义比较函数并将其应用于各行
def check_speed_diff(high,low,obs):
if np.isnan(obs):
return np.nan
elif np.isnan(high):
return low
elif np.isnan(low):
return high
if abs(high-obs)<abs(low-obs):
return high
else:
return low
df_old.apply(lambda x:
check_speed_diff(
x.forecastWind_high,
x.forecastWind_low,
x.obs_windSpeed
),
axis=1
)发布于 2020-09-04 21:07:41
这是另一种实现你想要的东西的方法。它允许不止两列进行比较。
col = ['forecastWind_low','forecastWind_high']
comparecol = ['obs_windSpeed']
df[col + comparecol] = df[col + comparecol].astype(float)
dfmerge =pd.merge(df[col].stack().reset_index(-1),df[comparecol],left_index=True,right_index=True,how='left')
dfmerge = dfmerge.rename(columns = {'level_1':'windforecast',0:'Amount'})
dfmerge['difference'] = abs(dfmerge['obs_windSpeed'] - dfmerge['Amount'])
dfmerge = dfmerge.sort_values(by='difference',ascending=True)
dfmerge = dfmerge.groupby(level=0).head(1)
df = pd.merge(df,dfmerge['Amount'],left_index=True,right_index=True,how='left')
df.loc[df['obs_windSpeed'].isna(),'Amount'] = np.nan发布于 2020-09-04 23:25:22
在修改Jeff的解决方案时,我设法想出了这个:
def check_speed_diff(high,low,obs):
if obs == 'NaN':
return np.nan
if low != 'NaN' and high == 'NaN':
return low
if low == 'NaN' and high != 'NaN':
return high
if low != 'NaN' and high != 'NaN':
if abs(high-obs)<abs(low-obs):
return high
else:
return low我遇到的另一个问题是一些列/行中的字符串不是'NaN',所以我使用pandas并强制执行错误:
df.forecast_WindSpeed_high = pd.to_numeric(df.forecast_WindSpeed_high,errors='coerce')
df.forecast_WindSpeed_low = pd.to_numeric(df.forecast_WindSpeed_low ,errors='coerce')使用Jeff建议的应用函数:
df['nearest_forecastWindSpeed'] = df.apply(lambda x: check_speed_diff(
x.forecast_WindSpeed_high,
x.forecast_WindSpeed_low,
x.windSpeed),axis=1)可能不是最有效率的,但我完成了工作...感谢大家的帮助。
https://stackoverflow.com/questions/63733381
复制相似问题