Pandas Float Object没有属性含义

时间:2018-03-03 16:53:36

标签: python pandas dataframe

我根据输入csv中的现有列在pandas中创建了一个新的计算字段,但我收到了此错误。

属性错误:("'浮动'对象没有属性'意思'",'发生在索引0')

# IMDB Data Formula
def weighted_rating(x):
    V = x['vote_count']
    R = x['vote_average']
    C = x['vote_average'].mean()
    m = x['vote_count'].quantile(0.10)
    # IMDB Formula
    return (V/(V+m) * R) + (m/(m+V) * C)

input_csv['w_average'] = input_csv.apply(weighted_rating, axis = 1)

vote_average&的示例数据vote_count:

df = pd.DataFrame({'vote_average': [7.2, 6.9, 6.3, 7.6, 6.1, 5.9, 7.4, 7.3, 7.4, 5.7, 5.4],
                   'vote_count': [11800, 4500, 4466, 9106, 2124, 3576, 3330, 6767, 5293, 7004, 1400]})

1 个答案:

答案 0 :(得分:1)

我相信你不需要apply,因为使用缓慢,更好:

V = input_csv['vote_count']
R = input_csv['vote_average']
C = input_csv['vote_average'].mean()
m = input_csv['vote_count'].quantile(0.10)

input_csv['w_average'] = (V/(V+m) * R) + (m/(m+V) * C)
print (input_csv)
    vote_average  vote_count  w_average
0            7.2       11800   7.116795
1            6.9        4500   6.821294
2            6.3        4466   6.414272
3            7.6        9106   7.421180
4            6.1        2124   6.377273
5            5.9        3576   6.181167
6            7.4        3330   7.109691
7            7.3        6767   7.145805
8            7.4        5293   7.186525
9            5.7        7004   5.922114
10           5.4        1400   6.156145