AttributeError: 'numpy.ndarray' object has no attribute 'apply'

22,517

There's two issues here.

  1. By taking .values you actually access the underlying numpy array; you no longer have a pandas.Series. numpy arrays do not have an apply method.
  2. You are trying to use apply for a simple multiplication, which will be orders of magnitude slower than using a vectorized approach.

See below:

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': np.arange(1000, dtype=np.float64)})
print(type(df['a']))
# Gives pandas.core.series.Series

print(type(df['a'].values))
# Gives numpy.ndarray

# The vectorized approach
df['a'] = df['a'] * 1.3
Share:
22,517

Related videos on Youtube

Michael Norman
Author by

Michael Norman

Updated on February 26, 2021

Comments

  • Michael Norman
    Michael Norman about 3 years
    Department = input("Is there a list you would like to view")
    
    
    readfile = pd.read_csv('6.csv')
    filevalues= readfile.loc[readfile['Customer'].str.contains(Department, na=False), 'June-18\nQty'] 
    filevalues = filevalues.fillna(int(0))
    
    int_series = filevalues.values.astype(int) 
    calculated_series = int_series.apply(lambda x: filevalues*1.3)
    
    
    print(filevalues)
    

    I am getting this error : AttributeError: 'numpy.ndarray' object has no attribute 'apply'

    I have looked through this website and no solutions seems to work. I simply want to multiply the data by 1.3 in this series. Thank you

    • rafaelc
      rafaelc almost 6 years
      int_series * 1.3?
    • Michael Norman
      Michael Norman almost 6 years
      @RafaelC I was attempting to multiply every value in my list by 1.3. I used this method because supposedly it is supposed to convert the series into an int.
    • roganjosh
      roganjosh almost 6 years
      int_series * 1.3 does multiply every value in the series by 1.3
    • Michael Norman
      Michael Norman almost 6 years
      @roganjosh Okay, but then do you know the reason for my error?
    • rafaelc
      rafaelc almost 6 years
      The reason is simple: there is no apply function in numpy arrays. There are, though, in pandas.Series objects, which you would have if you did filevalues.astype(int) instead of filevalues.values.astype(int)
    • Michael Norman
      Michael Norman almost 6 years
      @RafaelC Okay thank you, it worked! all i did was remove the .value.
    • rafaelc
      rafaelc almost 6 years
      Glad to help @HarisKhaliq :)