【问题标题】:how to fix the calculation error which says 'DataFrame' object is not callable如何修复显示“DataFrame”对象不可调用的计算错误
【发布时间】:2019-08-28 18:18:17
【问题描述】:

我正在处理足球数据集,这是我遇到的错误。请帮忙,

#what is the win rate of HomeTeam?





n_matches = df.shape[0]



n_features = df.shape[1] -1

n_homewin = len(df(df.FTR == 'H'))

win_rate = (float(n_homewin) / (n_matches)) * 100

print ("Total number of matches,{}".format(n_matches))
print ("Number of features,{}".format(n_features))
print ("Number of maches won by hom team,{}".format (n_homewin))
print ("win rate of home team,{:.2f}%" .format(win_rate))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-122-7e4d81fc684e> in <module>
      5 n_features = df.shape[1] -1
      6 
----> 7 n_homewin = len(df(df.FTR == 'H'))
      8 
      9 win_rate = (float(n_homewin) / (n_matches)) * 100

TypeError: 'DataFrame' 对象不是 预期结果应该打印团队胜率

【问题讨论】:

    标签: pandas jupyter-notebook sklearn-pandas


    【解决方案1】:

    我认为问题出在(),需要[] 来过滤boolean indexing

    n_homewin = len(df[df.FTR == 'H'])
    

    或者通过sum更简单地计算Trues 值:

    n_homewin = (df.FTR == 'H').sum()
    

    【讨论】:

    • 非常感谢您的帮助,先生,这确实如您所说。
    【解决方案2】:

    您应该将其修改为df[df.FTR == 'H']。括号表示函数调用

    【讨论】:

    • 非常感谢您,先生,这确实如您所说。
    猜你喜欢
    • 2021-09-23
    • 2019-10-04
    • 2019-12-06
    • 2019-10-05
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多