0

Below is a minimum reproducible example of what I'm trying to achieve with my custom function. The function works if I remove the if statement and second argument. I know the error is derived from the if statement, but I can't seem to figure out what the solution is.

df = pd.DataFrame({"odds":[100, -200, -400], "favorite":[0, 1, 1]})

def odd_bin_group_fn(odds, favorite):
    if (odds <= -200 and favorite == 1):
        return('large_favorite')
    else:
        return('other')

df['odd_bin'] = odd_bin_group_fn(df["odds"], df["favorite"])

2 Answers 2

2

Just use anonymous function and apply() method:

df['odd_bin']=df.apply(lambda x:'large_favorite' if (x['odds']<=-200) & (x['favorite']==1) else 'other',1)

OR

You can also use numpy's where() method:

df['odd_bin']=np.where((df['odds']<=-200) & (df['favorite']==1),'large_favorite','other')
Sign up to request clarification or add additional context in comments.

2 Comments

Assigned corect columns names.
I think best answer here - using np.where
1

To apply function on rows, you can use apply() with axis=1.

df['odd_bin'] = df.apply(lambda row: odd_bin_group_fn(row["odds"], row["favorite"]), axis=1)
print(df)

   odds  favorite         odd_bin
0   100         0           other
1  -200         1  large_favorite
2  -400         1  large_favorite

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.