AttributeError: 'numpy.ndarray' object has no attribute 'apply'
22,517
There's two issues here.
- By taking
.values
you actually access the underlyingnumpy
array; you no longer have apandas.Series
.numpy
arrays do not have anapply
method. - 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
Related videos on Youtube
Author by
Michael Norman
Updated on February 26, 2021Comments
-
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 almost 6 years
int_series * 1.3
? -
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 almost 6 years
int_series * 1.3
does multiply every value in the series by 1.3 -
Michael Norman almost 6 years@roganjosh Okay, but then do you know the reason for my error?
-
rafaelc almost 6 yearsThe reason is simple: there is no
apply
function innumpy
arrays. There are, though, inpandas.Series
objects, which you would have if you didfilevalues.astype(int)
instead offilevalues.values.astype(int)
-
Michael Norman almost 6 years@RafaelC Okay thank you, it worked! all i did was remove the .value.
-
rafaelc almost 6 yearsGlad to help @HarisKhaliq :)
-