Convert a 1D array to a 2D Numpy array Last Updated : 08 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Here we will learn how to convert 1D NumPy to 2D NumPy Using two methods. Numpy is a Python package that consists of multidimensional array objects and a collection of operations or routines to perform various operations on the array and processing of the array. Convert a 1D array to a 2D Numpy array using reshape This package consists of a function called numpy.reshape which is used to convert a 1-D array into a 2-D array of required dimensions (n x m). This function gives a new required shape without changing the data of the 1-D array. Examples 1: Convert a 1-D array into a 2-D array of required dimensions (n x m). Python3 import numpy as np # 1-D array having elements [1 2 3 4 5 6 7 8] arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) # Now we can convert this 1-D array into 2-D in two ways # 1. having dimension 4 x 2 arr1 = arr.reshape(4, 2) print ('After reshaping having dimension 4x2:') print (arr1) print ('\n') # 2. having dimension 2 x 4 arr2 = arr.reshape(2, 4) print ('After reshaping having dimension 2x4:') print (arr2) print ('\n') Output: After reshaping having dimension 4x2: [[1 2] [3 4] [5 6] [7 8]] After reshaping having dimension 2x4: [[1 2 3 4] [5 6 7 8]]Example 2: Let us see an important observation whether we can reshape a 1-D array into any 2-D array. Python3 import numpy as np # 1-D array having elements [1 2 3 4 5 6 7 8] arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) # Print the 1-D array print('Before reshaping:') print(arr) print('\n') # let us try to convert into 2-D array having dimension 3x3 arr1 = arr.reshape(3, 3) print('After reshaping having dimension 3x3:') print(arr1) print('\n') Output: This concludes that the number of elements should be equal to the product of dimension i.e. 3x3=9 but total elements = 8; Example 3: Another example is that we can use the reshape method without specifying the exact number for one of the dimensions. Just pass -1 as the value and NumPy will calculate the number. Python3 import numpy as np # 1-D array having elements [1 2 3 4 5 6 7 8] arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) # Print the 1-D array print('Before reshaping:') print(arr) print('\n') arr1 = arr.reshape(2, 2, -1) print('After reshaping:') print(arr1) print('\n') Output: Before reshaping: [1 2 3 4 5 6 7 8] After reshaping: [[[1 2] [3 4]] [[5 6] [7 8]]]Convert a 1D array to a 2D Numpy array using numpy.reshape Here, we are using np.reshape to convert a 1D array to 2 D array. You can divide the number of elements in your array by ncols. Python3 import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # np.reshape(A,(-1,ncols)) B = np.reshape(arr, (-1, 2)) print('2D Numpy array: \n', B) Output: 2D Numpy array: [[ 1 2] [ 3 4] [ 5 6] [ 7 8] [ 9 10] Comment More infoAdvertise with us Next Article Convert a 1D array to a 2D Numpy array T TanmayChakraborty Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads Convert 2D float array to 2D int array in NumPy Converting a 2D float array to a 2D integer array in NumPy is a straightforward process using the astype() method. This conversion can be useful in various data analysis and scientific computing tasks where integer data types are required or where memory efficiency is essential. In this article, we 8 min read NumPy | Multiply 2D Array to 1D Array Given two NumPy arrays, the task is to multiply a 2D array with a 1D array, each row corresponding to one element in NumPy. You can follow these methods to multiply a 1D array into a 2D array in NumPy: Using np.newaxis()Using axis as noneUsing transpose()Let's understand them better with Python prog 2 min read How to convert 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array. 2 min read Python | Flatten a 2d numpy array into 1d array Given a 2d numpy array, the task is to flatten a 2d numpy array into a 1d array. Below are a few methods to solve the task. Method #1 : Using np.flatten() Python3 # Python code to demonstrate # flattening a 2d numpy array # into 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5] 2 min read Convert a NumPy array to an image Converting a NumPy array to an image is a simple way to turn numbers into pictures. A NumPy array holds pixel values, which are just numbers that represent colors. Images, like PNG or JPEG, store these pixel values in a format we can see. In this process, the NumPy array turns into an image, with ea 3 min read Convert a NumPy array to a Pandas series Let us see how to convert a NumPy array to a Pandas series. A NumPy array can be converted into a Pandas series by passing it in the pandas.Series() function. Example 1 : Python3 # importing the modules import numpy as np import pandas as pd # creating an NumPy array array = np.array([10, 20, 1, 2, 1 min read Convert Numpy Array To Xarray Xarray is a powerful Python library for working with labeled multi-dimensional arrays. In Python, NumPy provides basic data structures and APIs for working with raw ND arrays, but, in the real world, the data is more complex, in some cases, which are encoded. The data array maps to positions in spac 3 min read How to get all 2D diagonals of a 3D NumPy array? Let's see the program for getting all 2D diagonals of a 3D NumPy array. So, for this we are using numpy.diagonal() function of NumPy library. This function return specified diagonals from an n-dimensional array. Syntax: numpy.diagonal(a, axis1, axis2)Parameters: a: represents array from which diag 3 min read How to access a NumPy array by column Accessing a NumPy-based array by a specific Column index can be achieved by indexing. NumPy follows standard 0-based indexing in Python.  Example:Given array: 1 13 6 9 4 7 19 16 2 Input: print(NumPy_array_name[ :,2]) Output: [6 7 2] Explanation: printing 3rd columnAccess ith column of a 2D Numpy Arr 3 min read How to convert 1-D arrays as columns into a 2-D array in Python? Let's see a program to convert 1-D arrays as columns into a 2-D array using NumPy library in Python. So, for solving this we are using numpy.column_stack() function of NumPy. This function takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. Syntax : numpy.column_stac 1 min read Like