3 回答

TA贡献1900条经验 获得超5个赞
您可以使用np.digitize以下方法实现它:
indeces = [None,] + thresholds.index.tolist()
scores["score"].apply(
lambda x: indeces[np.digitize(x, thresholds["threshold"])])

TA贡献1829条经验 获得超6个赞
您可以merge_asof通过一些操作来获得准确的结果。
(pd.merge_asof( scores.reset_index().sort_values('score'),
thresholds.reset_index(),
left_on='score', right_on= 'threshold', suffixes = ('','_'))
.drop('threshold',1).rename(columns={'index_':'threshold'})
.set_index('index').sort_index())
并使用您的数据,您将获得:
score threshold
index
0 -1.613293 NaN
1 -1.357980 NaN
2 0.325720 7.0
3 0.116000 NaN
4 1.423171 33.0
5 0.282557 7.0
6 -1.195269 NaN
7 0.395739 7.0
8 1.072041 33.0
9 0.197853 NaN
添加回答
举报