# Python: Split a 2d Array Vertically and Convert It into a 3d Array

Articles From: SH Fintech Modeling
Website: SH Fintech Modeling

This post shows how to split a 2d array into subsets of 2d arrays and convert them into a 3d array.

The following figure shows what we want to do in this post.

### Exercise with code snippets

For example, let’ consider m which is a 2d array.

```# slice vertically 2d array and convert them to 3d array

import numpy as np

m = np.array([[ 1, 2, 3, 4, 5, 6],
[11,12,13,14,15,16],
[21,22,23,24,25,26],
[31,32,33,34,35,36],
[41,42,43,44,45,46]])
m```
```array([[ 1,  2,  3,  4,  5,  6],
[11, 12, 13, 14, 15, 16],
[21, 22, 23, 24, 25, 26],
[31, 32, 33, 34, 35, 36],
[41, 42, 43, 44, 45, 46]])```

The following code examples split m (2d array) vertically into 2, 3, or 6 sub 2d arrays and concatenate them as a 3d array. The third argument of reshape() function is the number of sub columns. swapaxes(0,1) swaps .shape[0] and .shape[1].

The only thing we do is just to select the number of sub columns.

```# slice vertically 2d array and convert them to 3d array

#1
m.reshape(m.shape[0],-1, 3).swapaxes(0,1)
#2
m.reshape(m.shape[0],-1, 2).swapaxes(0,1)
#3
m.reshape(m.shape[0],-1, 1).swapaxes(0,1)```
```#1
array([[[ 1,  2,  3],
[11, 12, 13],
[21, 22, 23],
[31, 32, 33],
[41, 42, 43]],
[[ 4,  5,  6],
[14, 15, 16],
[24, 25, 26],
[34, 35, 36],
[44, 45, 46]]])
#2
array([[[ 1,  2],
[11, 12],
[21, 22],
[31, 32],
[41, 42]],
[[ 3,  4],
[13, 14],
[23, 24],
[33, 34],
[43, 44]],
[[ 5,  6],
[15, 16],
[25, 26],
[35, 36],
[45, 46]]])
#3
array([[[ 1],
[11],
[21],
[31],
[41]],

[[ 2],
[12],
[22],
[32],
[42]],

[[ 3],
[13],
[23],
[33],
[43]],

[[ 4],
[14],
[24],
[34],
[44]],

[[ 5],
[15],
[25],
[35],
[45]],

[[ 6],
[16],
[26],
[36],
[46]]])```

Originally posted on SH Fintech Modeling blog.

##### Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SH Fintech Modeling and is being posted with permission from SH Fintech Modeling. The views expressed in this material are solely those of the author and/or SH Fintech Modeling and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.