Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 546
Michael Norman Avatar asked Sep 12 '26 06:09

Michael Norman


1 Answers

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
like image 107
roganjosh Avatar answered Sep 13 '26 21:09

roganjosh



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!