英文:
ValueError: could not convert string to float while converting fractions
问题
I will provide translations for the code and the error message, as you requested. Here they are:
df.head()
country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
381084 Iceland League Cup 167/50 329/100 63/100 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
381085 Iceland League Cup 463/100 173/50 47/100 2016-02-14 21:15:00 Fram Stjarnan 0 3
381086 Iceland League Cup 9/25 99/25 303/50 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
381087 Iceland League Cup 9/25 393/100 611/100 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
381088 Iceland League Cup 11/25 353/100 251/50 2016-02-14 17:00:00 Akranes Grindavik 5 0
def convert(s):
if '/' in str(s): # is a fraction
num, den = s.split('/')
return 1 + (int(num) / int(den))
else:
return s.astype(float)
odds_cols = ['home_odds', 'draw_odds', 'away_odds']
df[odds_cols] = df[odds_cols].apply(convert)
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_2.py", line 70, in <module>
df[odds_cols] = df[odds_cols].apply(convert)....
return arr.astype(dtype, copy=True)
ValueError: could not convert string to float: '167/50'
If you need further assistance in resolving this error, please feel free to ask.
英文:
df.head()
country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
381084 Iceland League Cup 167/50 329/100 63/100 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
381085 Iceland League Cup 463/100 173/50 47/100 2016-02-14 21:15:00 Fram Stjarnan 0 3
381086 Iceland League Cup 9/25 99/25 303/50 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
381087 Iceland League Cup 9/25 393/100 611/100 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
381088 Iceland League Cup 11/25 353/100 251/50 2016-02-14 17:00:00 Akranes Grindavik 5 0
I am applying the following function:
def convert(s):
if '/' in str(s): # is a fraction
num, den = s.split('/')
return 1 + (int(num) / int(den))
else:
return s.astype(float)
odds_cols = ['home_odds', 'draw_odds', 'away_odds']
df[odds_cols] = df[odds_cols].apply(convert)
I am getting the below error:
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_2.py", line 70, in <module>
df[odds_cols] = df[odds_cols].apply(convert)....
return arr.astype(dtype, copy=True)
ValueError: could not convert string to float: '167/50'
How do I resolve it?
答案1
得分: 1
apply
提供了一行或一列给函数,而你想要逐个元素地发送该行。使用 applymap
,如下所示的代码(我假设 1.0 + 用于显示所需的赔率)。如果 DataFrame 包含不能转换的元素,基本错误检查会将这些元素替换为 NaN,并且这些行可以打印出来供你检查。
def convert(s):
if isinstance(s, str):
if '/' in str(s): # 是一个分数
num, den = s.split('/')
try:
val = 1.0 + int(num) / int(den)
except ValueError:
val = None
else:
try:
val = float(s)
except ValueError:
val = None
else:
val = None
return val
df[odds_cols] = df[odds_cols].applymap(convert)
print(df[df.isna().any(axis=1)]) # 除非需要检查,否则可以省略
英文:
apply
provides a row or column to the function whereas you want the row sent element by element. Use applymap
as in the following code (I assume 1.0 + is needed for how you want odds shown). In case the DF includes elements which cannot be converted, basic error checking replaces these with NaN and such rows can then printed for you to check.
def convert(s):
if isinstance(s, str):
if '/' in str(s): # is a fraction
num, den = s.split('/')
try:
val = 1.0 + int(num) / int(den)
except ValueError:
val = None
else:
try:
val = float(s)
except ValueError:
val = None
else:
val = None
return val
df[odds_cols] = df[odds_cols].applymap(convert)
print(df[df.isna().any(axis = 1)]) # omit unless checking wanted
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论