计算定义错误?

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

Definition calculating incorrectly?

问题

下面是代码部分的翻译结果,只返回翻译好的内容,不包括其他内容:

  1. # 带有分数的数据帧
  2. # 应返回该分数的浮点值。
  3. df:
  4. Unnamed: 0 country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
  5. 0 381084 Iceland League Cup 167/50 329/100 63/100 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
  6. 1 381085 Iceland League Cup 463/100 173/50 47/100 2016-02-14 21:15:00 Fram Stjarnan 0 3
  7. 2 381086 Iceland League Cup 9/25 99/25 303/50 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
  8. 3 381087 Iceland League Cup 9/25 393/100 611/100 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
  9. 4 381088 Iceland League Cup 11/25 353/100 251/50 2016-02-14 17:00:00 Akranes Grindavik 5 0
  10. # 使用以下代码进行转换:
  11. def convert(s):
  12. if '/' in str(s): # 是一个分数
  13. num, den = s.split('/')
  14. return 1 + int(num) / int(den)
  15. else:
  16. return float(s)
  17. odds_cols = ['home_odds', 'draw_odds', 'away_odds']
  18. df[odds_cols] = df[odds_cols].applymap(convert)

希望这能帮助你正确返回所需的值。

关于您的最后一个问题,Pandas 将 "167/50" 识别为 float 的原因是因为它在内部执行了自动类型转换。当您使用 applymap 函数时,Pandas 会尝试将每个单元格的值解析为适当的数据类型。在这种情况下,它能够识别 "167/50" 作为分数并将其转换为浮点数。这是 Pandas 数据框的一个强大功能,可以自动识别和处理各种数据类型。

英文:

I have a dataframe with fractions.

It should return the float value of that fraction.

  1. df:
  2. Unnamed: 0 country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
  3. 0 381084 Iceland League Cup 167/50 329/100 63/100 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
  4. 1 381085 Iceland League Cup 463/100 173/50 47/100 2016-02-14 21:15:00 Fram Stjarnan 0 3
  5. 2 381086 Iceland League Cup 9/25 99/25 303/50 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
  6. 3 381087 Iceland League Cup 9/25 393/100 611/100 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
  7. 4 381088 Iceland League Cup 11/25 353/100 251/50 2016-02-14 17:00:00 Akranes Grindavik 5 0

I am using:

  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 float(s)
  7. odds_cols = ['home_odds', 'draw_odds', 'away_odds']
  8. df[odds_cols] = df[odds_cols].applymap(convert)

White it returns

  1. Unnamed: 0 country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
  2. 0 381084 Iceland League Cup 3.34 3.29 0.63 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
  3. 1 381085 Iceland League Cup 4.63 3.46 0.47 2016-02-14 21:15:00 Fram Stjarnan 0 3
  4. 2 381086 Iceland League Cup 0.36 3.96 6.06 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
  5. 3 381087 Iceland League Cup 0.36 3.93 6.11 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
  6. 4 381088 Iceland League Cup 0.44 3.53 5.02 2016-02-14 17:00:00 Akranes Grindavik 5 0

It should return

  1. Unnamed: 0 country league home_odds draw_odds away_odds datetime home_team away_team home_score away_score
  2. 0 381084 Iceland League Cup 4.34 4.29 1.63 2016-02-16 19:15:00 Kopavogur Vikingur Reykjavik 0 1
  3. 1 381085 Iceland League Cup 5.63 4.46 1.47 2016-02-14 21:15:00 Fram Stjarnan 0 3
  4. 2 381086 Iceland League Cup 1.36 4.96 7.06 2016-02-14 19:15:00 KR Reykjavik Haukar 1 1
  5. 3 381087 Iceland League Cup 1.36 4.93 7.11 2016-02-14 19:00:00 Thor Akureyri Leiknir F. 5 0
  6. 4 381088 Iceland League Cup 1.44 4.53 6.02 2016-02-14 17:00:00 Akranes Grindavik 5 0

Since odds_cols have float and int values too, I cannot just apply return 1 + float(s)

What is the correct way to return the values as desired?

Also, when I type

  1. print(df.dtypes)
  2. Unnamed: 0 int64
  3. country object
  4. league object
  5. home_odds float64
  6. draw_odds float64
  7. away_odds float64
  8. datetime object
  9. home_team object
  10. away_team object
  11. home_score int64
  12. away_score int64
  13. dtype: object

How come pandas identifies the value 167/50 as float?

答案1

得分: 2

如果home_oddsdraw_oddsaway_odds中的数据以字符串格式显示,那么我可以从您的确切函数中获得您想要的结果。

我会尝试将odds_cols强制转换为字符串,例如:

  1. df['home_odds'] = df['home_odds'].astype(str)

然后查看输出。

英文:

If the data in home_odds, draw_odds and away_odds is as shown in the strings format then I get exactly what you want from your exact function.

I'd test casting the odds_cols to strings e.g.

  1. df['home_odds'] = df['home_odds'].astype(str)

and see the output.

答案2

得分: 1

你的函数可能不会在捕获分数的条件下执行:

  1. def convert(s):
  2. if '/' in str(s): # 是一个分数
  3. # 试着在这里加入一个打印语句
  4. num, den = s.split('/')
  5. return 1 + int(num) / int(den)
  6. else:
  7. return float(s)

很可能你总是会进入 float(s) 的执行路径,因为框架中保留的是数字而不是字符串。

英文:

your function likely doesn't get executed on the condition to catch the fraction

  1. def convert(s):
  2. if '/' in str(s): # is a fraction
  3. # TRY ADDING a print statement here
  4. num, den = s.split('/')
  5. return 1 + int(num) / int(den)
  6. else:
  7. return float(s)

Likely you always get the float(s) execution path, all because the frame keeps the numbers, not strings.

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

发表评论

匿名网友

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

确定