
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Append Values to an Array
Append Values to an Arrays in NumPy
Appending values to an array in NumPy means adding new elements or arrays to an existing NumPy array. This operation involves creating a new array that includes the original elements along with the new ones, as NumPy arrays have fixed sizes and do not support in-place modifications like traditional lists.
Appending Values to a 1D Array
Appending values to a 1D array in NumPy involves adding new elements to the end of an existing one-dimensional array. Since NumPy arrays have a fixed size, this operation creates a new array with the original elements plus the newly appended values. To achieve this, we can use the np.append() function in NumPy. Following is the syntax −
numpy.append(arr, values, axis=None)
Where,
- arr: The original array to which values will be appended.
- values: The values to be appended. Can be a single value or an array.
- axis: The axis along which values are appended. For 1D arrays, this parameter is ignored and can be None.
Example
In the following example, we are using the np.append() to add elements to a 1D array: first appending a single value, and then multiple values −
import numpy as np # Create an initial 1D array arr = np.array([1, 2, 3]) # Append a single value arr_appended_single = np.append(arr, 4) # Append multiple values arr_appended_multiple = np.append(arr, [4, 5, 6]) print("Array after appending a single value:", arr_appended_single) print("Array after appending multiple values:", arr_appended_multiple)
Following is the output obtained −
Array after appending a single value: [1 2 3 4] Array after appending multiple values: [1 2 3 4 5 6]
Appending Values to a 2D Array
Appending values to a 2D array in NumPy involves adding rows or columns to an existing two-dimensional array. Since NumPy arrays have a fixed size, appending values results in creating a new array that combines the original data with the new data. Let us explore various ways and methods used to append values to a 2D array −
Appending 2D Rows
To append rows to a 2D array, we can use the np.vstack() function. This function stacks arrays vertically or concatenate along axis 0. Following is the syntax −
numpy.vstack(tup)
Where, tup is a sequence of arrays to be stacked vertically. All arrays must have the same number of columns.
Example
In this example, we are using the np.vstack() function to append rows to a 2D array, adding new rows beneath the existing ones −
import numpy as np # Create an initial 2D array arr = np.array([[1, 2], [3, 4]]) # Create an array of rows to append rows_to_append = np.array([[5, 6], [7, 8]]) # Append rows to the initial array arr_appended_rows = np.vstack((arr, rows_to_append)) print("Array after appending rows:") print(arr_appended_rows)
The result is a new array with the additional rows stacked at the bottom −
Array after appending rows: [[1 2] [3 4] [5 6] [7 8]]
Appending 2D Columns
To append columns to a 2D array, we can use the np.hstack() function. This function stacks arrays horizontally or concatenate along axis 1. Following is the syntax −
numpy.hstack(tup)
Where, tup is a sequence of arrays to be stacked horizontally. All arrays must have the same number of rows.
Example
In the example below, we are using the np.hstack() function to append columns to a 2D array, adding new columns to the right of the existing ones −
import numpy as np # Create an initial 2D array arr = np.array([[1, 2], [3, 4]]) # Create an array of columns to append columns_to_append = np.array([[5], [6]]) # Append columns to the initial array arr_appended_columns = np.hstack((arr, columns_to_append)) print("Array after appending columns:") print(arr_appended_columns)
The result is a new array with the additional columns placed alongside the original data −
Array after appending columns: [[1 2 5] [3 4 6]]
Appending to Multi-dimensional Arrays
Appending to multi-dimensional arrays in NumPy involves adding new elements along specified axes. Unlike 1D and 2D arrays, multi-dimensional arrays (e.g., 3D or higher) require careful alignment of the dimensions and axes along which you want to append data.
Example
In the following example, we are using the np.append() function to add values to a 3D array along the first axis −
import numpy as np # Original 3D array arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Values to append (must match dimensions) values = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]) # Append values along axis 0 result = np.append(arr, values, axis=0) print(result)
The resulting array has the new values appended as additional layers on top of the original array −
[[[ 1 2] [ 3 4]] [[ 5 6] [ 7 8]] [[ 9 10] [11 12]] [[13 14] [15 16]]]
Appending with Different Data Types
Appending arrays with different data types in NumPy requires careful handling because NumPy arrays are homogeneous; they must contain elements of the same data type.
When appending arrays with different data types, NumPy will generally perform type coercion or create a new array with a common data type that can contains all the appended data.
Example
In this example, we are using the np.append() to add floating-point values to an integer array −
import numpy as np # Original array of integers arr = np.array([1, 2, 3]) # Values to append (floating-point) values = np.array([4.5, 5.5]) # Append values result = np.append(arr, values) print(result)
After executing the above code, we get the following output −
[1. 2. 3. 4.5 5.5]
Append Using np.concatenate() Function
We can use the np.concatenate() function for combining arrays along a specified axis. It is particularly useful for appending data to existing arrays. Following is the syntax −
numpy.concatenate((a1, a2, ...), axis=0)
Where,
- a1, a2, ...: Arrays to be concatenated. They must have the same shape except in the dimension corresponding to the axis parameter.
- axis: The axis along which to concatenate the arrays. 0 is the default for 1D arrays, and other values for multi-dimensional arrays.
Example
In the following example, we are concatenating two 2D arrays along axis "0" using the np.concatenate() function −
import numpy as np # Arrays to concatenate arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Concatenate along axis 0 result = np.concatenate((arr1, arr2), axis=0) print(result)
The result produced is as follows −
[[1 2] [3 4] [5 6] [7 8]]