Skip to content
Advertisement

Is there a quick way in python to convert a string ‘1/100’ to float 0.01?

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.

Advertisement

Answer

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

df['col_name'] = pd.eval(target=df, expr=df['col_name'])
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement