How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘drop’

by | Programming, Python, Tips

If you attempt to call the drop() method on a NumPy array, you will raise the AttributeError: ‘numpy.ndarray’ object has no attribute ‘drop’. The drop() method belongs to the DataFrame object. This error typically occurs when you assign a NumPy array to a variable name that belongs to a DataFrame.

You can solve this error by converting the NumPy array to a DataFrame using the pandas.Dataframe(), then you can call the drop() method.

This tutorial will go through the error and how to solve it with code examples.


AttributeError: ‘numpy.ndarray’ object has no attribute ‘drop’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The drop method is a DataFrame method, not a numpy.ndarray method that removes rows or columns by specifying label names and corresponding axis or specifying index or column names.

Example

Suppose we want to use the StandardScaler on a DataFrame. StandardScaler removes the mean and scales each feature/variable to unit variance. First, we will define a DataFrame that contains random integers between 0 and 1000 in five columns and ten rows.

import pandas as pd
import numpy as np
import random

from sklearn.preprocessing import StandardScaler

df = pd.DataFrame(np.random.randint(0,1000,size=(10,5)),
   columns=['col1','col2','col3','col4','col5'],
   dtype='float64')

df
 col1   col2   col3   col4   col5
0  501.0  209.0  759.0  814.0  520.0
1  154.0  319.0  261.0  744.0  181.0
2  965.0  965.0  174.0  216.0  645.0
3  374.0  348.0  335.0  557.0  925.0
4  521.0  960.0  570.0  353.0  343.0
5  101.0  254.0    3.0  419.0  292.0
6  429.0  207.0  139.0  622.0  747.0
7   56.0  200.0  532.0  925.0  433.0
8  595.0  774.0  988.0  352.0  899.0
9  152.0  429.0   75.0  214.0  229.0

Next, we will fit and then transform the data.

df = StandardScaler().fit_transform(df)

Then we will try to drop the last two rows of the DataFrame.

df = df.drop([8,9], axis=0)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [10], in <cell line: 3>()
      1 df = StandardScaler().fit_transform(df)
----> 3 df = df.drop([8,9], axis=0)

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

The error occurs because StandardScaler.fit_transform() returns a NumPy array, not a DataFrame, and the NumPy array does not have drop as an attribute.

Solution

We can solve this error by converting the NumPy array to a DataFrame using pandas.DataFrame().

new_df = pd.DataFrame(StandardScaler().fit_transform(df), columns=df.columns, index=df.index)

new_df
 col1      col2      col3      col4      col5
0  0.435767 -0.871191  1.234817  1.230408 -0.005428
1 -0.865534 -0.499032 -0.403273  0.935851 -1.319732
2  2.175835  1.686558 -0.689445 -1.285953  0.479198
3 -0.040502 -0.400917 -0.159862  0.148962  1.564759
4  0.510770  1.669642  0.613133 -0.709462 -0.691658
5 -1.064292 -0.718944 -1.251922 -0.431737 -0.889385
6  0.165756 -0.877957 -0.804572  0.422479  0.874652
7 -1.233048 -0.901640  0.488138  1.697492 -0.342727
8  0.788281  1.040354  1.988076 -0.713670  1.463957
9 -0.873034 -0.126872 -1.015090 -1.294369 -1.133636

Once we have a DataFrame, we can use the drop() method:

new_df = new_df.drop([8,9], axis=0)

new_df
col1      col2      col3      col4      col5
0  0.435767 -0.871191  1.234817  1.230408 -0.005428
1 -0.865534 -0.499032 -0.403273  0.935851 -1.319732
2  2.175835  1.686558 -0.689445 -1.285953  0.479198
3 -0.040502 -0.400917 -0.159862  0.148962  1.564759
4  0.510770  1.669642  0.613133 -0.709462 -0.691658
5 -1.064292 -0.718944 -1.251922 -0.431737 -0.889385
6  0.165756 -0.877957 -0.804572  0.422479  0.874652
7 -1.233048 -0.901640  0.488138  1.697492 -0.342727

We successfully removed the last two rows from the DataFrame containing standardised values.

Summary

Congratulations on reading to the end of this tutorial! If you encounter this error, ensure that you check the type of object throwing the error using the type() method. If the object is a NumPy ndarray, you can convert it to a DataFrame using pandas.DataFrame().

For further reading on AttributeErrors involving NumPy ndarrays, go to the article:

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee