0
$\begingroup$

I have the following code, where sp_col is a sliced column of my dataframe df_1:

from sklearn.impute import SimpleImputer
import numpy as np

imputer = SimpleImputer(missing_values=np.NaN,strategy='mean')
sp_col =df_1.iloc[:-1,9:28].values.reshape(-1,1)

# print(sp_col.isnull().sum())

for items,j in sp_col.items():    
    im2 = sp_col
    im2 = imputer.fit(im2)
    im3 = imputer.transform(sp_col) 

I then get the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-207-28b34f875d63> in <module>
      8 # print(sp_col.isnull().sum())
      9 
---> 10 for items,j in sp_col.items():
     11         im2 = sp_col
     12         im2 = imputer.fit(im2)

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

   
$\endgroup$

2 Answers 2

0
$\begingroup$

Try this:

from sklearn.impute import SimpleImputer
import numpy as np

imputer = SimpleImputer(missing_values=np.NaN,strategy='mean')

df_1.iloc[:, 9:28] = si.fit_transform(df_1.iloc[:, 9:28])
$\endgroup$
1
  • $\begingroup$ thanks @shrinidhi M $\endgroup$ Aug 20, 2021 at 14:10
0
$\begingroup$

As the error says, you are trying to use the .items() attribute which numpy arrays do not have. .items() is a method for pandas series, but since you converted it to a numpy array that doesn't work anymore. Either keep the data as a pandas series or change your loop to for value in sp_col:.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.