0
df:

             score_difference   DNB selection selection_match
0                    1.040000  0.65       DNB        No Match
1                    0.894543  0.65       DNB        No Match
2                    2.120546  2.11       DNB        No Match
3                    0.672945  0.65       DNB        No Match
4                    1.578659  0.96       DNB        No Match
5                    2.746971  2.72       DNB        No Match

I am trying to write a function if selection_match == 'No Match' then DNB = score_difference + 0.02

def selection_mod_dnb(row):
    if row["selection_match"] == "No Match" and row['selection'] == "DNB":
        return 0.02 + float(row['score_difference'])
    else:
        return row['DNB']

df['DNB'] = df.apply(selection_mod_dnb, axis=1)

However, I return the same df without any modifications

df:

             score_difference   DNB selection selection_match
0                    1.040000  0.65       DNB        No Match
1                    0.894543  0.65       DNB        No Match
2                    2.120546  2.11       DNB        No Match
3                    0.672945  0.65       DNB        No Match
4                    1.578659  0.96       DNB        No Match
5                    2.746971  2.72       DNB        No Match

while it should return

             score_difference       DNB selection selection_match
0                    1.040000  1.060000       DNB        No Match
1                    0.894543  0.914543       DNB        No Match
2                    2.120546  2.140546       DNB        No Match
3                    0.672945  0.692945       DNB        No Match
4                    1.578659  1.598659       DNB        No Match
5                    2.746971  2.766971       DNB        No Match

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

5
  • it seems that you don't have a predicted_score_difference column in your dataframe Commented Jun 7, 2022 at 12:44
  • Corrected the question Commented Jun 7, 2022 at 12:44
  • It works fine with me. Commented Jun 7, 2022 at 12:50
  • It does not at my end. For some reason I cannot seem to understand Commented Jun 7, 2022 at 13:08
  • Is there any better/other way to rewrite this function? Commented Jun 7, 2022 at 13:10

1 Answer 1

1

Try using apply when doing row based calculations:

df['DNB'] = df.apply(lambda x: x.score_difference + 0.02 if x.selection_match == "No Match" else x.DNB, axis=1)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.