Home > Net >  How to Convert Decimal Odds to American Odds using List Comprehension in Python?
How to Convert Decimal Odds to American Odds using List Comprehension in Python?

Time:03-31

I am trying to create a new column named 'American' which converts Decimal odds to American odds. The column 'Odds' is currently in Decimal format.

Here is my dataframe, df:

import pandas as pd
import numpy as np

# create a dataframe
df = pd.DataFrame({
    'Name': ['Siraj', 'Emma', 'Alex', 'Maya', 'Lupin'],
    'Odds': [2, 2.5, 3, 3.5, 4]
})
# display the dataframe
print(df)

Here is my desired output displayed in a new dataframe, new_df:

new_df = pd.DataFrame({
    'Name': ['Siraj', 'Emma', 'Alex', 'Maya', 'Lupin'],
    'Odds': [2, 2.5, 3, 3.5, 4],
    'American': [100, 150, 200, 250, 300]
})
# display the dataframe
print(new_df)

If you want to check out the conversions yourself just enter the Decimal odds and this site will convert to American odds: https://www.actionnetwork.com/betting-calculators/betting-odds-calculator

Here is the formula for converting Decimal odds to American:

  • For Decimal odds of 2.0 or greater the formula is: (decimal odds - 1) * 100 = American Odds
  • For Decimal odds between 1.01 and 1.99 the formula is: -100 / (decimal odds - 1) = American Odds

Here is my attempt at coding the logic:

if df['Odds'] >= 2.0:
    df['American'] == (df['Odds'] - 1) * 100
else:
    df['American'] == -100 / (df['Odds'] - 1)

I found some possible solutions to my problem on this site: https://datascienceparichay.com/article/pandas-create-column-based-on-condition/

I tried implementing basic formulas for starters but was unsuccessful. Here are the attempts that I have tried so far:

  1. Where

    # create a new column based on condition
    df['odds'] = np.where(df['Age'] >= 16, df['Age'] == df['Age']   1, df['Age'] == df['Age'] - 1)
    # display the dataframe
    print(df)
    
  2. List Comprehension

    # create a new column based on condition
    df['american'] = [1   1 if a >= 3 else 1 - 1 for a in df['Odds']]
    # display the dataframe
    print(df)
    
  3. Function

    # create a function
    def get_american(Odds):
        if Odds >= 3:
            return df['Odds']   1
        else:
            return df['Odds'] - 1
    # create a new column based on condition
    df['american'] = df['Odds'].apply(get_american)
    # display the dataframe
    print(df)
    
  4. Dictionary Mapping (only method I didn't attempt)

    # create new column using ditionary mapping
    df['Is_adult'] = df['Is_eligible'].map({True: 'Yes', False: 'No'})
    # display the dataframe
    print(df)
    

CodePudding user response:

Your #3 was very close:

import pandas as pd

df = pd.DataFrame({
    'Name': ['Siraj', 'Emma', 'Alex', 'Maya', 'Lupin'],
    'Odds': [2, 2.5, 3, 3.5, 4]
})

print(df)
def mapping(odds):
    if odds >= 2:
        return (odds - 1) * 100
    else:
        return -100 / (odds-1)

df['American'] = df['Odds'].apply(mapping)
print(df)

If you'd rather use a single line, you can use the fact that True/False are equal to 1 and 0:

df['American'] = (df['Odds']-1)*100 * (df['Odds'] >= 2)   (-100/(df['Odds']-1) * (df['Odds'] < 2))

CodePudding user response:

You were on the right track with using np.where.

import pandas as pd
import numpy as np

# create a dataframe
df = pd.DataFrame({
    'Name': ['Siraj', 'Emma', 'Alex', 'Maya', 'Lupin'],
    'Odds': [2, 2.5, 3, 3.5, 4]
})

df['American'] = np.where(df['Odds'] >= 2.0, (df['Odds'] - 1) * 100, -100 / (df['Odds'] - 1))

# display the dataframe
print(df)

Output:

    Name  Odds  American
0  Siraj   2.0     100.0
1   Emma   2.5     150.0
2   Alex   3.0     200.0
3   Maya   3.5     250.0
4  Lupin   4.0     300.0
  • Related