How to Fix: ‘numpy.ndarray’ object has no attribute ‘append’

28 Nov, 2021
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. If you are into analytics, you might have come across this library in python. In the beginning, some things might confuse a programmer when switching from python traditional lists to NumPy arrays. One such error that we might come across is “AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’“.  In this article, let us look at why do we see this error and how to fix it.
When adding an item to a python list, we make use of the list’s append method. The syntax is pretty simple and when we try to replicate the same on a NumPy array we get the above-mentioned error. Let us look through it with an example.
Example: Code that depicts the error
Python
# Append method on python lists  import numpyprint("-"*15, "Python List", "-"*15)  # Create a python listpylist = [1, 2, 3, 4]  # View the data type of the list objectprint("Data type of python list:", type(pylist))  # Add (append) an item to the python listpylist.append(5)  # View the items in the listprint("After appending item 5 to the pylist:", pylist)  print("-"*15, "Numpy Array", "-"*15)  # Append method on numpy arrays  # Import the numpy library  # Create a numpy arraynplist = numpy.array([1, 2, 3, 4])  # View the data type of the numpy arrayprint("Data type of numpy array:", type(nplist))  # Add (append) an item to the numpy arraynplist.append(5)
Output:
Numpy Append Error
In the above output, we can see that the python list has a data type of list. When we perform the append operation, the item i.e., 5 gets appended to the end of the list `pylist`. While we try the same method for the NumPy array, it fails and throws an error “AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’“. The output is pretty explanatory, the NumPy array has a type of numpy.ndarray which does not have any append() method. 
Now, we know that the append is not supported by NumPy arrays then how do we use it? It is actually a method of NumPy and not its array, let us understand it through the example given below, where we actually perform the append operation on a numpy list.
Syntax:
numpy.append(arr, values, axis=None)
Parameters:
arr: numpy array: The array to which the values are appended to as a copy of it.
values: numpy array or value: These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis). If axis is not specified, values can be any shape and will be flattened before use.
axis: int, optional: The axis along which values are appended. If axis is not given, both arr and values are flattened before use.
Example: Fixed code 
Python
# Append method on numpy arrays  # Import the numpy libraryimport numpy  # Create a numpy arraynplist = numpy.array([1, 2, 3, 4])  # View the data type of the numpy arrayprint("Data type of numpy array:", type(nplist))  # View the items in the numpy arrayprint("Initial items in nplist:", nplist)  # Add (append) an item to the numpy arraynplist = numpy.append(nplist, 5)  # View the items in the numpy arrayprint("After appending item 5 to the nplist:", nplist)
Output:
Numpy Append Output
As in the output, we can see that initially, the NumPy array had 4 items (1, 2, 3, 4). After appending 5 to the list, it is reflected in the NumPy array. This is so because here the append function is used on NumPy and not on NumPy array object (numpy.ndarray). 
Picked
Python How-to-fix
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function