-1

I have read in a numpy.ndarray that looks like this:

[[1.4600e-01 2.9575e+00 6.1580e+02]
 [5.8600e-01 4.5070e+00 8.7480e+02]]

Let's assume that this array I am reading will not always have a length of 2. (e.g. It could have a length of 1,3, 456, etc.)

I would like to separate this to two separate arrays that look like this:

[[1.4600e-01 2.9575e+00 6.1580e+02]]

[[5.8600e-01 4.5070e+00 8.7480e+02]]

I previously tried searching a solution to this problem but this is not the solution I am looking for: python convert 2d array to 1d array

Dila
  • 649
  • 1
  • 10
  • What exactly do you mean? You haven't shown what the actual output will look like in terms of actual python objects. The printouts can be obtained in any number of ways. – Mad Physicist Nov 07 '22 at 22:40

1 Answers1

1

Since you want to extract the rows, you can just index them. So suppose your array is stored in the variable x. x[0] will give you the first row: [1.4600e-01 2.9575e+00 6.1580e+02], while x[1] will give you the second row: [5.8600e-01 4.5070e+00 8.7480e+02], etc.

You can also iterate over the rows doing something like:

for row in x:
  # Do stuff with the row

If you really want to preserve the outer dimension, you reshape the rows using x[0].reshape((1,-1)) which says to set the first dimension to 1 (meaning it has 1 row) and infer the second dimension from the existing data.

Alternatively if you want to split some number of rows into n groups, you can use the numpy.vsplit() function: https://numpy.org/doc/stable/reference/generated/numpy.vsplit.html#numpy.vsplit

However, I would suggest looping over the rows instead of splitting them up unless you really need to split them up.

Kirby
  • 15
  • 6