• Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Facebook Twitter Instagram
  • links
Biz Assurance
Subscribe
  • Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Biz Assurance
Home»Tech»Convert 1d Array to 2d Array Python
Tech

Convert 1d Array to 2d Array Python

Alicia CormieBy Alicia CormieNo Comments3 Mins Read
Facebook Twitter WhatsApp
Share
Facebook Twitter LinkedIn WhatsApp

Like other programming languages, Python also contains several built-in data structures, for instance, arrays. Arrays can be one-dimensional, two-dimensional, or depending on the user’s requirements, provide a way to organize and save data. Additionally, you can use the built-in Python methods to retrieve or modify that data.

This post will illustrate the different methods for converting a 1d array into a 2d array.

How to Convert 1d Array to 2d Array in Python?

To perform the converting operation on a one-dimensional array to a two-dimensional array in Python, the “reshape()” function can be utilized that enables users to reshape an array. More specifically, reshaping is known as changing the shape of a given array which is determined by the number of elements in every dimension without changing its data.

Syntax

Utilize the below-stated syntax for the reshape() method:

input_string.reshape(row, col)

Here, the “input_string” is the variable name that contains the value of the array, the “row” and “col” are two parameters that are passed to the “reshape” function.

Example 1: Convert 1d Array to 2d Array in Python Using “reshape()” Method With “np.arange()” Method

To convert a one-dimension array to a two-dimensional array, first import the Numpy library as np:

import numpy as np

Now, create an array variable and initialize it with the term “original_array”. Then, assign a value by utilizing an “np.arange()” function:

original_array = np.arange(8)

Here, “arange()” is the function of the “np” library, and we have passed a value “8” as the function parameter.

Print the previously created array using the “print()” function:

print(“Original array: n“, original_array)

Next, create a variable that will keep the converted array. Then, call the “reshape()” method by passing a desired number of rows and columns as an argument to associate this method with the specified variable:

resultant_array = original_array.reshape(2, 4)

Finally, print the converted array:

print(“Converted array: n“, resultant_array)

It can be seen that the specified one-dimensional array has been successfully converted to a two-dimensional array:

Example 2: Convert 1d Array to 2d Array in Python Using “np.reshape()” Method

Another way to convert the 1d array into the 2d array is to utilize the “np.reshape()” function. To do so, first, create a new 1d input array:

array_list = np.array([0, 1, 2, 3, 4, 5, 6, 7])

Then, print the 1d array:

print(“Original array: n“, array_list)

Now, call the “np.reshape()” method with a 1d input array by passing the parameters. Here, the “-1” refers to the size of the specified input array:

resultant_string = np.reshape(array_list, (–1, 2))

Print the resultant 2d array from the np.reshape() method:

print(“Converted array: n“, resultant_string)

Output

That’s all about converting a 1d array into a 2d array in Python through different methods.

Conclusion

To convert a 1d array into a 2d array in Python, the “np.reshape()” method and “reshape()” method with the “np.arange()” method can be used. Both methods enable users to reshape the input array without modifying its data. This post illustrated the different methods for converting a 1d array into a 2d array.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleHow Do You Repeat a String n Times in Python?
Next Article Divide Two Columns Pandas

Related Posts

How to Fix “Not connected – No connections are available” Error in Windows

How to Fix “Blurry Font Problem” in Windows 10

How to Fix “Can’t create new folder” in Windows 10

Add A Comment

Leave A Reply Cancel Reply

Does Texas Lawmaker Ted Cruz Really Not Consider El Paso To Be A Border City?

A Canadian Festival Features Harry Styles And Doja Cat… Except It Appears To Be Fake And Has Made Thousands In Ticket Sales

Jimmy Kimmel, The Slap, And Crisis Response Teams: Your 2023 Oscars Drinking Game

Bad Bunny Will Be One Of The Final ‘Carpool Karaoke’ Guests Before James Corden’s ‘The Late Late Show’ Ends

  • Homepage
  • Sitemap
© 2023 Biz Assurance - Designed by Curtiex Ventures.

Type above and press Enter to search. Press Esc to cancel.