在数据帧上运行函数时,Python TypeError“系列对象不能解释为整数”

问题描述 投票:0回答:1

我正在尝试运行此代码:

def fact(x):
    if x in [0, 1]:
        return 1
    r = 1
    for a in range(1, (x+1)):  
        r = r*a
    return r
 
def ch(a, b):
    return fact(a)/(fact(b)*fact(a-b))

def matchProb(pW, W, L, best_of):
    #pW is the probability of winning a set
    #W is the winner's set score (they won 1,2, 3 set etc)
    #L is the loser's set score
    #best_of is the total numner  of sets
    
    towin = (best_of + 1) / 2 #best of three is whoever wins two
    Wleft = towin - W
    remain = best_of - W - L 
    
    Wleft.where(Wleft == 0, 1)
    
    Wleft.where(Wleft > remain, 1)
       
    win = 0
    for i in range(Wleft, (remain+1)):
        add = ch((i-1), (Wleft-1))*(pW**(Wleft-1))*((1-pW)**(i-Wleft))*pW
        win += add
    return win
 

当我运行它时,出现以下错误

df1[test] = matchProb(.5*Richtennis['W_Skill'], Richtennis['Wset1'], Richtennis['Lset1'], Richtennis['best_of'])

为什么会发生这种情况,我该如何解决? 当我使用直接值时不会发生这种情况

matchProb(0.5, 1, 0, 3)
打印出 0.665

非常感谢帮助!

python pandas data-science
1个回答
0
投票

您正在处理 pandas 系列并尝试将其作为整数处理。另外,您需要确保您拥有正确的 dptype。以下是改进和纠正方法的建议:

import numpy as np
import pandas as pd

def fact(x):
    x = x.astype(int)
    result = np.ones_like(x, dtype=float)  
    max_val = np.max(x)  
    factorials = np.ones(max_val+1, dtype=float)
    for i in range(2, max_val+1):
        factorials[i] = factorials[i-1] * i
    result = factorials[x]  
    return result


def ch(a, b):
    return fact(a) / (fact(b) * fact(a - b))

def matchProb(pW, W, L, best_of):
    towin = ((best_of + 1) // 2).astype(int)  
    Wleft = np.maximum(1, towin - W).astype(int)
    remain = (best_of - W - L).astype(int)
    
    win = np.zeros_like(pW, dtype=float)  
    for i in range(1, int(np.max(remain))+1):  
        win_contribution = ch(i+Wleft-1, Wleft-1) * (pW**Wleft) * ((1-pW)**(i-1))
        win += win_contribution
    return win


np.random.seed(42)  
data = {
    'W_Skill': np.random.uniform(0.5, 1.5, size=10),
    'Wset1': np.random.randint(0, 4, size=10),
    'Lset1': np.random.randint(0, 3, size=10),
    'best_of': np.random.choice([3, 5], size=10)
}

Richtennis = pd.DataFrame(data)
print(Richtennis)

Richtennis['test'] = Richtennis.apply(lambda row: matchProb(0.5 * row['W_Skill'], row['Wset1'], row['Lset1'], row['best_of']), axis=1)

我在这里创建了一个小样本数据框:

    W_Skill  Wset1  Lset1  best_of
0  0.874540      1      0        5
1  1.450714      0      0        5
2  1.231994      1      1        5
3  1.098658      3      1        5
4  0.656019      3      0        5
5  0.655995      1      0        3
6  0.558084      1      0        5
7  1.366176      1      2        5
8  1.101115      3      2        3
9  1.208073      3      2        5

应用函数返回:

    W_Skill  Wset1  Lset1  best_of                test
0  0.874540      1      0        5  1.1177533239777162
1  1.450714      0      0        5  2.2258720975881996
2  1.231994      1      1        5  1.4198505830249353
3  1.098658      3      1        5  0.5493292420985183
4  0.656019      3      0        5  0.5484285262904508
5  0.655995      1      0        3  0.5484123176584215
6  0.558084      1      0        5  0.6319234059189444
7  1.366176      1      2        5   1.376840802623449
8  1.101115      3      2        3                 0.0
9  1.208073      3      2        5                 0.0
© www.soinside.com 2019 - 2024. All rights reserved.