Python pandas lamba add new column with value if any is equal to reference column

Viewed 20

I have a code like this that add 2 other columns and gives me the closest higher and closest lower value from reference value 'last_price'.

But I have noticed that if the 'last_price' is equal to any value on the row, this value is being ignored.

                df_merged = df_merged[
                    ['symbol', 'last_price', 'PP_S8', 'PP_S7_5', 'PP_S7', 'PP_S6_5', 'PP_S6', 'PP_S5_5', 'PP_S5',
                     'PP_S4_5', 'PP_S4',
                     'PP_S3_5',
                     'PP_S3', 'PP_S2_5', 'PP_S2', 'PP_S1_5', 'PP_S1', 'PP_S_5', 'PP_S_PP_R', 'PP_R_5', 'PP_R1',
                     'PP_R1_5',
                     'PP_R2', 'PP_R2_5',
                     'PP_R3', 'PP_R3_5', 'PP_R4', 'PP_R4_5', 'PP_R5', 'PP_R5_5', 'PP_R6', 'PP_R6_5',
                     'PP_R7', 'PP_R7_5',
                     'PP_R8']].assign(n_closest_s=tmp[tmp.apply(lambda x: x < tmp["last_price"])].max(axis=1),
                                      n_closest_r=tmp[tmp.apply(lambda x: x > tmp["last_price"])].min(axis=1)

Now i have added this but it doesn't work, I want it to add a new column, the value if 'last_price' is equal to any value in the row:

n_equal__r=tmp[tmp.apply(lambda x: x == tmp["last_price"])].eq(axis=1) 
0 Answers
Related