1

So I have a pandas data frame consisting of data from boxing matches and their odds of winning. It's columns are:

[Red_fighter, Blue_fighter, Red_odds, Blue_odds, winner]

I want to change it so that if for example, blue's odds are 'less' than red's that blue gets added to column 'Favourite' and red gets added to 'Underdog' both of which replace 'Red_fighter' and 'Blue fighter'

[favourite, underdog, favourite_odds, underdog_odds, winner]

So if I have:

{'Red_fighter' : 'Tom Jones', 'Blue_fighter' : 'Jack Jackson', 'Red_odds' : 200, 'Blue_odds' : -200 , 'Winner' : 'Blue'}

It becomes:

{'Underdog' : 'Tom Jones', 'Favourite' : 'Jack Jackson', 'Red_odds' : 200, 'Blue_odds' : -200 , 'Winner' : 'Favourite'}

I appreciate any help you can give, I'm a newbie to pandas and data analytics in general, thanks!

1 Answer 1

3

You can achieve this using the pd.Series.where method:

df['Underdog'] = df.Red_fighter.where(df.Red_odds < df.Blue_odds, df.Blue_fighter)
df['Favourite'] = df.Red_fighter.where(df.Red_odds > df.Blue_odds, df.Blue_fighter)

df['Underdog_odds'] = df.Red_odds.where(df.Red_odds < df.Blue_odds, df.Blue_odds)
df['Favourite_odds'] = df.Red_odds.where(df.Red_odds > df.Blue_odds, df.Blue_odds)

This method works by replacing the values where a condition is NOT satisfied with values from another series. The remaining values which satisfy the condition are left untouched.

So for example if we have df.A.where(cond, df.B), all rows where cond is True will have values from A and all rows where cond is False will have values from B. There is more information in the documentation.

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

2 Comments

Also, would this make the 'Winner' column automatically have the data of 'Underdog' or 'Favourite' instead of Red or Blue?
No but you could make it in the same manner: df.Winner = df.Red_fighter.where(df.Winner == 'Red', df.Blue_fighter). Also if the answer helped your question please mark it as accepted to conclude the thread :)

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.