0

I have this df:

    Home    Away    5+ corners  6+ corners  7+ corners  8+ corners  9+ corners  10+ corners 11+ corners 12+ corners 13+ corners 14+ corners
0   Aston Villa Leeds   1/100   1/50    1/20    1/8 1/4 4/9 4/6 11/10   13/8    5/2
1   West Ham    Manchester City 1/100   1/33    1/12    1/6 1/3 4/7 10/11   11/8    2/1 3/1
2   Fulham  Crystal Palace  1/66    1/33    1/10    1/5 2/5 4/6 11/10   7/4 5/2 4/1
3   Manchester Utd  Chelsea 1/66    1/33    1/8 1/5 2/5 8/11    11/10   15/8    11/4    4/1
4   Liverpool   Sheffield Utd   1/100   1/50    1/20    1/8 2/7 4/9 8/11    11/10   7/4 5/2
5   Southampton Everton 1/66    1/33    1/12    1/5 4/11    8/13    1/1 13/8    5/2 7/2
6   Wolves  Newcastle   1/50    1/20    1/8 2/7 1/2 4/5 5/4 2/1 3/1 5/1
7   Arsenal Leicester   1/100   1/33    1/12    1/6 1/3 1/2 10/11   11/8    2/1 3/1
8   Brighton    West Brom   1/66    1/33    1/8 1/4 2/5 4/6 11/10   7/4 11/4    4/1
9   Burnley Tottenham   1/100   1/33    1/12    1/6 1/3 4/7 10/11   11/8    9/4 3/1

which I would like to convert to decimal odds.

I know i could use .split('/') to achieve this but was wondering if there was a quicker way to do this.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58

3 Answers3

1

As suggested by @ch3steR, use pd.eval and try this

df['col_name'] = pd.eval(target=df, expr=df['col_name'])
Ajay A
  • 1,030
  • 1
  • 7
  • 19
0

What about the eval function?

result = eval('1/2')

Edit: As commenters have pointed out, eval has security concerns, since it has the potential to execute arbitrary code.

mzndr
  • 91
  • 8
0

you can use pandas apply and eval together

df.loc[:,'5+ corners':] = df.loc[:,'5+ corners':].apply(pd.eval)

>>> print(df)
>>>
        Home            Away     5+ corners  ... 12+ corners 13+ corners 14+ corners
0     AstonVilla           Leeds       0.01  ...         1.1       1.625         2.5
1        WestHam  ManchesterCity       0.01  ...       1.375           2           3
2         Fulham   CrystalPalace  0.0151515  ...        1.75         2.5           4
3  ManchesterUtd         Chelsea  0.0151515  ...       1.875        2.75           4
4      Liverpool    SheffieldUtd       0.01  ...         1.1        1.75         2.5

This will only work for dataframe upto 100 rows , refer here

Shijith
  • 4,602
  • 2
  • 20
  • 34