Appending 2D Arrays in Python
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python.
This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python.
Use append()
the function to append values to a 2D array in Python
In this case, we will use a list instead of an array. Lists are one of the four built-in data types provided in Python and are very similar to arrays. You can first tolist()
convert a NumPy array to a list using the function.
append()
The function is used to add an item to the end of the specified list. This function does not create a new list but modifies the original list.
The following code uses append()
the append function to append a two-dimensional array in Python.
a = [[], []]
a[0].append([10, 20])
a[1].append([80, 90])
print(a)
Output:
[[[10, 20]], [[80, 90]]]
First a two-dimensional list is created in the above code and then we append()
add the required elements using the function. It adds the provided value at the end of the list.
We can numpy.array()
convert the final result into a NumPy array using the function.
numpy.append()
Append values to a 2D array in Python
using the
The NumPy library handles multidimensional arrays and provides functions that perform smooth operations on the arrays given in the code.
We can use the function when creating an array numpy.array()
. The NumPy module contains a function numpy.append()
that is used to append an element to the end of a given array.
numpy.append()
method has the following syntax.
numpy.append(arr, values, axis=None)
Note that if no axis values are provided, the multidimensional array is flattened, resulting in a single-dimensional array. Also, the provided values need to have a similar shape to the given array.
The following code uses numpy.append()
the append function to append a two-dimensional array in Python.
import numpy as np
arr5 = np.array([[10, 20, 30], [100, 200, 300]])
arr6 = np.array([[70, 80, 90], [310, 320, 330]])
newselect = np.append(arr5, arr6, axis=1)
print(newselect)
Output:
[[ 10 20 30 70 80 90]
[100 200 300 310 320 330]]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si
Calculating Mahalanobis distance in Python
Publish Date:2025/05/05 Views:185 Category:Python
-
This tutorial will show you how to find the Mahalanobis distance between two NumPy arrays in Python. Use the function scipy.spatial.distance in the Python library cdist() to calculate the Mahalanobis distance The Mahalanobis distance is a m
Implementing the ReLU function in Python
Publish Date:2025/05/05 Views:177 Category:Python
-
This tutorial will discuss the Relu function and how to implement it in Python. ReLU function Relu Functions are the foundation of machine learning and are essential when using deep learning. ReLU The term is Rectified Linear Unit an acrony
Killing a Python process
Publish Date:2025/05/05 Views:152 Category:Python
-
When programming in Python, sometimes our program gets stuck in an infinite loop. In this case, we need to terminate the program manually. This article will discuss different ways to kill a Python process. Killing a Python process using a k
Get file extension in Python
Publish Date:2025/05/05 Views:63 Category:Python
-
This tutorial shows how to get the file extension from a file name in Python. os.path Extract extension from file using module in Python The Python module os.path pre-makes useful utility functions for manipulating operating system file pat
Read the first line of a file in Python
Publish Date:2025/05/05 Views:192 Category:Python
-
In Python, we have built-in functions to handle different file operations. A text file contains a sequence of strings where each line \n is terminated by a newline character. In this tutorial, we will learn how to read the first line of a t
Reading binary files in Python
Publish Date:2025/05/05 Views:119 Category:Python
-
The program or internal processor interprets the binary file. It contains bytes as content. When we read a binary file, an bytes object of type is returned. open() Read a binary file using the function in Python In Python, we have open() th
Writing bytes to a file in Python
Publish Date:2025/05/05 Views:193 Category:Python
-
In this tutorial, we will look at how to write bytes to a binary file in Python. Binary files contain strings of type bytes. When we read a binary file, an object of type bytes is returned. In Python, bytes are represented by hexadecimal nu
Python get file name without extension from path
Publish Date:2025/05/05 Views:81 Category:Python
-
This tutorial will demonstrate various ways to get the file name without extension from a file path in Python. Suppose our goal is to get the file name from a list of file paths that are in the form of strings, such as from the path Desktop