ValueError: 在转换分数时无法将字符串转换为浮点数

huangapple go评论155阅读模式
英文:

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:

  1. df.head()
  2. country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
  3. 381084 Iceland League Cup 167/50 329/100 63/100 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
  4. 381085 Iceland League Cup 463/100 173/50 47/100 2016-02-14 21:15:00 Fram Stjarnan 0 3
  5. 381086 Iceland League Cup 9/25 99/25 303/50 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
  6. 381087 Iceland League Cup 9/25 393/100 611/100 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
  7. 381088 Iceland League Cup 11/25 353/100 251/50 2016-02-14 17:00:00 Akranes Grindavik 5 0
  1. def convert(s):
  2. if '/' in str(s): # is a fraction
  3. num, den = s.split('/')
  4. return 1 + (int(num) / int(den))
  5. else:
  6. return s.astype(float)
  7. odds_cols = ['home_odds', 'draw_odds', 'away_odds']
  8. df[odds_cols] = df[odds_cols].apply(convert)
  1. Traceback (most recent call last):
  2. File "C:\Users\user\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_2.py", line 70, in <module>
  3. df[odds_cols] = df[odds_cols].apply(convert)....
  4. return arr.astype(dtype, copy=True)
  5. ValueError: could not convert string to float: '167/50'

If you need further assistance in resolving this error, please feel free to ask.

英文:
  1. df.head()
  2. country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
  3. 381084 Iceland League Cup 167/50 329/100 63/100 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
  4. 381085 Iceland League Cup 463/100 173/50 47/100 2016-02-14 21:15:00 Fram Stjarnan 0 3
  5. 381086 Iceland League Cup 9/25 99/25 303/50 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
  6. 381087 Iceland League Cup 9/25 393/100 611/100 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
  7. 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:

  1. def convert(s):
  2. if &#39;/&#39; in str(s): # is a fraction
  3. num, den = s.split(&#39;/&#39;)
  4. return 1 + (int(num) / int(den))
  5. else:
  6. return s.astype(float)
  7. odds_cols = [&#39;home_odds&#39;, &#39;draw_odds&#39;, &#39;away_odds&#39;]
  8. df[odds_cols] = df[odds_cols].apply(convert)

I am getting the below error:

  1. Traceback (most recent call last):
  2. File &quot;C:\Users\user\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_2.py&quot;, line 70, in &lt;module&gt;
  3. df[odds_cols] = df[odds_cols].apply(convert)....
  4. return arr.astype(dtype, copy=True)
  5. ValueError: could not convert string to float: &#39;167/50&#39;

How do I resolve it?

答案1

得分: 1

apply 提供了一行或一列给函数,而你想要逐个元素地发送该行。使用 applymap,如下所示的代码(我假设 1.0 + 用于显示所需的赔率)。如果 DataFrame 包含不能转换的元素,基本错误检查会将这些元素替换为 NaN,并且这些行可以打印出来供你检查。

  1. def convert(s):
  2. if isinstance(s, str):
  3. if '/' in str(s): # 是一个分数
  4. num, den = s.split('/')
  5. try:
  6. val = 1.0 + int(num) / int(den)
  7. except ValueError:
  8. val = None
  9. else:
  10. try:
  11. val = float(s)
  12. except ValueError:
  13. val = None
  14. else:
  15. val = None
  16. return val
  17. df[odds_cols] = df[odds_cols].applymap(convert)
  18. 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.

  1. def convert(s):
  2. if isinstance(s, str):
  3. if &#39;/&#39; in str(s): # is a fraction
  4. num, den = s.split(&#39;/&#39;)
  5. try:
  6. val = 1.0 + int(num) / int(den)
  7. except ValueError:
  8. val = None
  9. else:
  10. try:
  11. val = float(s)
  12. except ValueError:
  13. val = None
  14. else:
  15. val = None
  16. return val
  17. df[odds_cols] = df[odds_cols].applymap(convert)
  18. print(df[df.isna().any(axis = 1)]) # omit unless checking wanted

huangapple
  • 本文由 发表于 2023年2月27日 06:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定