【发布时间】:2020-09-04 23:25:21
【问题描述】:
这是一个更大数据集的样本:
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]})
【问题讨论】:
-
你尝试了什么?
-
尝试按照这个例子 (stackoverflow.com/questions/53969800/…) 但我认为附加的不相关列和某些行中的 NaN/错误字符串是问题...
-
重要的是您告诉我们您尝试过做什么。一段代码通常很有用。
df=df_old.fillna(0)然后df['nearest_forecast_windSpeed']=np.where(df.obs_windSpeed.sub(df.forecastWind_low)<df.obs_windSpeed.sub(df.forecastWind_high),df.forecastWind_low,df.forecastWind_high)
标签: python python-3.x pandas