gpt4 book ai didi

python - Dataframe 计算给出 AttributeError : float object has no attribute mean

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:56 24 4
gpt4 key购买 nike

我正在 pandas 中创建一个新的计算字段,基于输入 csv 中每一行中的现有列,但我收到此错误:AttributeError: ("'float' object has no attribute 'mean' ", '发生在索引 0')

代码:

def weighted_rating(x):
"""IMDB Data Formula"""
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_averagevote_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]})

最佳答案

我相信你不需要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

关于python - Dataframe 计算给出 AttributeError : float object has no attribute mean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com