[SOLVED] 'numpy.float64' object has no attribute 'abs'

Issue

I have this dataframe and code.

from pandas import DataFrame
import pandas as pd
import numpy as np

df = pd.DataFrame({'userId': [10,20,10,20,10,20,10,20],
                   'movieId': [500,500,800,800,700,700,1100,1100],  
                   'ratings': [4.5,4.5,2.0,2.0,4.0,1.5,3.5,2.5]})
def finding_rating(df):


    r = df.pivot(index="movieId",columns="userId")
    r.columns = ["u1","u2"]                                                                                                

    r["drate"] = r.u1.sub(r.u2).abs()
    v = r.drate.iloc[:-1].mean()-r.drate.iloc[-1].abs()
    print(r,v)

finding_rating(df)

I’m trying to take abs() value of vbut it’s giving this error. 'numpy.float64' object has no attribute 'abs'

Solution

Because working with scalars use:

v = abs(r.drate.iloc[:-1].mean()-r.drate.iloc[-1])

Or

v = (r.drate.iloc[:-1].mean()-r.drate.iloc[-1]).__abs__()

Answered By – jezrael

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *