Home > Enterprise >  Reshape 3-d array to 2-d
Reshape 3-d array to 2-d

Time:07-02

I want to change my array type as pd.DataFrame but its shape is:

array_.shape
(1, 181, 12)

I've tried to reshape by the following code, but it didn't work:

new_arr = np.reshape(array_, (-1, 181, 12))

How can I change its shape?

CodePudding user response:

You may want to erase the -1 parameter from the reshape() function to get a final output shape of (181, 12)

import numpy as np

m = np.array([[[1]*12]*181])
n = np.reshape(m, (181, 12))
print(m.shape, n.shape)

Output:

(1, 181, 12) (181, 12)

CodePudding user response:

NumPy array dimensions can be reduced using various ways; some are:
using np.squeeze:

array_.squeeze(0)

using np.reshape:

array_.reshape(array_.shape[1:])

or with using -1 in np.reshape:

array_.reshape(-1, m.shape[-1])

# new_arr = np.reshape(array_, (-1, 12))    # <==   change to your code

using indexing as Szczesny's comment:

array_[0]
  • Related