{"id":901,"date":"2024-02-23T13:22:26","date_gmt":"2024-02-23T13:22:26","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=901"},"modified":"2024-02-23T13:22:29","modified_gmt":"2024-02-23T13:22:29","slug":"numpy-joining-array","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/numpy-joining-array\/","title":{"rendered":"NumPy\u00a0Joining Array"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Joining NumPy Arrays<\/h2>\n\n\n\n<p>Joining means putting contents of two or more arrays in a single array.<\/p>\n\n\n\n<p>In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.<\/p>\n\n\n\n<p>We pass a sequence of arrays that we want to join to the&nbsp;<code>concatenate()<\/code>&nbsp;function, along with the axis. If axis is not explicitly passed, it is taken as 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Join two arrays<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr1 = np.array(&#91;1,\u00a02,\u00a03])\n\narr2 = np.array(&#91;4,\u00a05,\u00a06])\n\narr = np.concatenate((arr1, arr2))\n\nprint(arr)\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Join two 2-D arrays along rows (axis=1):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr1 = np.array(&#91;&#91;1,\u00a02], &#91;3,\u00a04]])<br><br>arr2 =\u00a0np.array(&#91;&#91;5,\u00a06], &#91;7,\u00a08]])<br><br>arr = np.concatenate((arr1, arr2), axis=1)<br><br>print(arr)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Joining Arrays Using Stack Functions<\/h2>\n\n\n\n<p>Stacking is same as concatenation, the only difference is that stacking is done along a new axis.<\/p>\n\n\n\n<p>We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.<\/p>\n\n\n\n<p>We pass a sequence of arrays that we want to join to the&nbsp;<code>stack()<\/code>&nbsp;method along with the axis. If axis is not explicitly passed it is taken as 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr1 = np.array(&#91;1,\u00a02,\u00a03])\n\narr2 =\u00a0np.array(&#91;4,\u00a05,\u00a06])\n\narr = np.stack((arr1, arr2), axis=1)\n\nprint(arr)\n\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Stacking Along Rows<\/h2>\n\n\n\n<p>NumPy provides a helper function:&nbsp;<code>hstack()<\/code>&nbsp;to stack along rows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr1 = np.array(&#91;1,&nbsp;2,&nbsp;3])<br><br>arr2 = np.array(&#91;4,&nbsp;5,&nbsp;6])<br><br>arr = np.hstack((arr1, arr2))<br><br>print(arr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_join4\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Stacking Along Columns<\/h2>\n\n\n\n<p>NumPy provides a helper function:&nbsp;<code>vstack()&nbsp;<\/code>&nbsp;to stack along columns.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr1 = np.array(&#91;1,\u00a02,\u00a03])\n\narr2 = np.array(&#91;4,\u00a05,\u00a06])\n\narr = np.vstack((arr1, arr2))\n\nprint(arr)\n\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Stacking Along Height (depth)<\/h2>\n\n\n\n<p>NumPy provides a helper function:&nbsp;<code>dstack()<\/code>&nbsp;to stack along height, which is the same as depth.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr1 = np.array(&#91;1,\u00a02,\u00a03])<br><br>arr2 = np.array(&#91;4,\u00a05,\u00a06])<br><br>arr = np.dstack((arr1, arr2))<br><br>print(arr)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Test Yourself With Exercises<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Exercise:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Use a correct NumPy method to join two arrays into a single array.arr1 = np.array(&#91;1, 2, 3]) arr2 = np.array(&#91;4, 5, 6]) arr = np.((arr1, arr2))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Joining NumPy Arrays Joining means putting contents of two or more arrays in a single array. In SQL we join tables based on a key, whereas in NumPy we join arrays by axes. We pass a sequence of arrays that we want to join to the&nbsp;concatenate()&nbsp;function, along with the axis. If axis is not explicitly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-901","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=901"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/901\/revisions"}],"predecessor-version":[{"id":902,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/901\/revisions\/902"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}