Round values of a python dataframe column according to authorized values
问题 I have this dataframe : df = pd.DataFrame({'id':[1,2,3,4], 'score':[0.35,3.4,5.5,8]}) df id score 0 1 0.35 1 2 3.4 2 3 5.5 3 4 8 and this list : L = list(range(1,7)) L [1, 2, 3, 4, 5, 6] I would like to round the values of df.scores to the closest value in L. Consequently, I would like to get : df id score 0 1 1 1 2 3 2 3 6 3 4 6 I tried something like df['score'].apply(lambda num : min([list(range(1,7)), key = lambda x:abs(x-num)]) but it didn't work (I'm a very beginner, sorry if this