AlwaysJunior
AlwaysJunior

Reputation: 83

Reshape 3-d array to 2-d

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?

Upvotes: 0

Views: 56

Answers (2)

Ali_Sh
Ali_Sh

Reputation: 2826

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]

Upvotes: 2

Cardstdani
Cardstdani

Reputation: 5223

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)

Upvotes: 0

Related Questions