Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe calculation giving AttributeError: float object has no attribute mean

I'm creating a new calculated field in pandas, based on existing columns in each row from the input csv but I'm getting this error: AttributeError: ("'float' object has no attribute 'mean'", 'occurred at index 0')

Code:

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)

Example data for vote_average and 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]})
like image 690
Parminder Rana Avatar asked Dec 18 '25 09:12

Parminder Rana


1 Answers

I believe you dont need apply, because slow, better is use:

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
like image 156
jezrael Avatar answered Dec 20 '25 21:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!