[Solved] AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’

If you use the regular Python list append() method to add an element to the end of NumPy array, you will encounter AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’.

In this tutorial, we will look at what exactly is AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ and how to resolve this issue with examples.

What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’?

In Python, it is common to use the append() method to add an element to the end of the array like we do in the list.

Similarly, if we attempt to append one or more elements to the end of the NumPy array using the regular list append() method, we will encounter AttributeError.

The NumPy arrays are different compared to the regular Python lists. Let us take a simple example to demonstrate the issue.

# import numpy library
import numpy as np

# define NumPy array
cars = np.array(["Toyota","Volkswagen","Tesla","Ford"])

# append the new car into numpy array
cars.append("Chevrolet")

# print the appended cars
print(cars)

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 8, in <module>
    cars.append("Chevrolet")
AttributeError: 'numpy.ndarray' object has no attribute 'append'

If you look at the output error message, it’s self-explanatory that the NumPy array has a type numpy.ndarray which does not have an append() attribute.

How to fix AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’?

Now that we have understood that we cannot use the append() method to add an element to the numpy.ndarray like we do it on the regular list, let us check the other options that we have to solve the issue. 

The NumPy has its own built-in method called numpy.append(), which can be used to append or add an item to the NumPy array.

Syntax of numpy.append()

numpy.append(arrvaluesaxis=None)

Parameters of numpy.append()

  • arr – The NumPy array to which the values need to be appended. Values are appended to a copy of this array.
  • values – These values are appended to a copy of arr. It must be in the correct shape (the same shape as arr, excluding axis). If the axis is not specified, values can be any shape and will be flattened before use.
  • axis (optional) – The axis along which values are appended. If the axis is not given, both arr and values are flattened before use.

Return Value of numpy.append()

The numpy.append() method returns a new array after appending the specified items to the end of an array. The output array is flattened if the axis is not provided or None.

Let us use the np.append() and fix the AttributeError issue that occurred in our example.

Example – Append the items to NumPy array using numpy.append() method

# import numpy library
import numpy as np

# define NumPy array
cars = np.array(["Toyota", "Volkswagen", "Tesla", "Ford"])

# append the new cars into numpy array
cars = np.append(cars, ["Chevrolet", "Jeep"])

# print the appended cars
print(cars)

Output

['Toyota' 'Volkswagen' 'Tesla' 'Ford' 'Chevrolet' 'Jeep']

We use the term np to refer to the NumPy library. It is an alias to the NumPy itself, as we have defined this while importing the NumPy library.

The above examples execute successfully and append the items into the end of the numpy array.

Conclusion

The AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ occurs if you attempt to append an element into NumPy array using the regular append() method like we do in the list. The numpy.ndarray does not have an append method, and hence it throws AttributeError.

We can resolve this error by using the numpy.append() method provided by the NumPy library. The numpy.append() method returns a copy of an array with values appended to the specified axis. If the axis is not specified, it will return a flattened array.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Dir()

Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module…
View Post