3 回答

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以使用np.digitize以下方法實(shí)現(xiàn)它:
indeces = [None,] + thresholds.index.tolist()
scores["score"].apply(
lambda x: indeces[np.digitize(x, thresholds["threshold"])])

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以merge_asof通過一些操作來獲得準(zhǔn)確的結(jié)果。
(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())
并使用您的數(shù)據(jù),您將獲得:
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
添加回答
舉報(bào)