如何解决无法调用“ DataFrame”对象的计算错误

时间:2019-04-07 12:50:12

标签: pandas jupyter-notebook sklearn-pandas

我正在研究足球数据集,而这是我收到的错误消息。请帮助

#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”对象不是     预期结果应显示球队胜率

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我认为问题是(),需要[]才能被boolean indexing过滤:

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

或者将True的值简化为sum

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