计算定义错误?

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

Definition calculating incorrectly?

问题

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

# 带有分数的数据帧
# 应返回该分数的浮点值。

df:
   Unnamed: 0  country      league home_odds draw_odds away_odds             datetime      home_team           away_team  home_score  away_score
0      381084  Iceland  League Cup    167/50   329/100    63/100  2016-02-16 19:15:00      Kopavogur  Vikingur Reykjavik           0           1
1      381085  Iceland  League Cup   463/100    173/50    47/100  2016-02-14 21:15:00           Fram            Stjarnan           0           3
2      381086  Iceland  League Cup      9/25     99/25    303/50  2016-02-14 19:15:00   KR Reykjavik              Haukar           1           1
3      381087  Iceland  League Cup      9/25   393/100   611/100  2016-02-14 19:00:00  Thor Akureyri          Leiknir F.           5           0
4      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):  # 是一个分数
        num, den = s.split('/')
        return 1 + int(num) / int(den)
    else:
        return float(s)

odds_cols = ['home_odds', 'draw_odds', 'away_odds']

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.

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

I am using:

def convert(s):
    if '/' in str(s):  # is a fraction
        num, den = s.split('/')
        return 1 + int(num) / int(den)
    else:
        return float(s)

odds_cols = ['home_odds', 'draw_odds', 'away_odds']

df[odds_cols] = df[odds_cols].applymap(convert)

White it returns

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

It should return

   Unnamed: 0  country      league  home_odds  draw_odds  away_odds             datetime      home_team           away_team  home_score  away_score
0      381084  Iceland  League Cup       4.34       4.29       1.63  2016-02-16 19:15:00      Kopavogur  Vikingur Reykjavik           0           1
1      381085  Iceland  League Cup       5.63       4.46       1.47  2016-02-14 21:15:00           Fram            Stjarnan           0           3
2      381086  Iceland  League Cup       1.36       4.96       7.06  2016-02-14 19:15:00   KR Reykjavik              Haukar           1           1
3      381087  Iceland  League Cup       1.36       4.93       7.11  2016-02-14 19:00:00  Thor Akureyri          Leiknir F.           5           0
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

print(df.dtypes)


Unnamed: 0      int64
country        object
league         object
home_odds     float64
draw_odds     float64
away_odds     float64
datetime       object
home_team      object
away_team      object
home_score      int64
away_score      int64
dtype: object

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

答案1

得分: 2

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

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

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.

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

and see the output.

答案2

得分: 1

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

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

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

英文:

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

def convert(s):
    if '/' in str(s):  # is a fraction
        # TRY ADDING a print statement here 
        num, den = s.split('/')
        return 1 + int(num) / int(den)
    else:
        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:

确定