[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.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
How To Copy A File In Python

How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess…
View Post
Nxnxn Matrix Python

nxnxn matrix python

Table of Contents Hide What is NxNxN?How to Create NxNxN Matrix in Python?Create NxN Matrix in Python 3 with Non Duplicating numbersCreate NxNxN matrix in Python using numpy In this…
View Post
Python Ord() A Step-By-Step Guide

Python ord(): A Step-By-Step Guide

Table of Contents Hide ord() Function in Python Example ord() vs chr() ord() Function ExamplesTypeError: ord() expected a character, but string of length 2 found. In Python ord() function accepts a single…
View Post
How To Check If A File Exists In Python

How to check if a file exists in Python?

Table of Contents Hide Python check if a file exists using OS ModuleUsing os.path.exists()Using os.path.isfile()Using os.path.isdir()Using pathlib.Path.exists() When you perform a file operation such as reading from a file or…
View Post