我正在研究足球数据集,而这是我收到的错误消息。请帮助
#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”对象不是 预期结果应显示球队胜率
答案 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()