

{"id":79309,"date":"2020-07-27T10:48:05","date_gmt":"2020-07-27T05:18:05","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79309"},"modified":"2021-05-09T13:13:31","modified_gmt":"2021-05-09T07:43:31","slug":"stacking-and-joining-in-numpy","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/","title":{"rendered":"Stacking and Joining in NumPy"},"content":{"rendered":"<p>Stacking is the concept of joining arrays in NumPy. Arrays having the same dimensions can be stacked. The stacking is done along a new axis. Stacking leads to increased customization of arrays. We can combine the stack function with other functions to further increase its capabilities.<\/p>\n<h2>Stacking and Joining in NumPy<\/h2>\n<p>NumPy implements the function of stacking. We can perform stacking along three dimensions:<\/p>\n<ul>\n<li><strong>vstack() \u2013<\/strong> it performs vertical stacking along the rows.<\/li>\n<li><strong>hstack()\u2013<\/strong> it performs horizontal stacking along with the columns.<\/li>\n<li><strong>dstack()\u2013<\/strong> it performs in-depth stacking along a new third axis.<\/li>\n<\/ul>\n<p>NumPy stack function takes two input parameters \u2013 the arrays for stacking and the axis for the resultant array.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nnp.stack(array,axis)\r\n<\/pre>\n<h2>Joining in NumPy<\/h2>\n<p>We use joining functions to append and concatenate elements and arrays. The concatenate <strong>()function<\/strong> is for joining arrays along a new axis.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\n arr1 = np.array([[1, 2], [5, 6]])\r\narr2 = np.array([[7, 8], [3,4]])\r\na = np.concatenate((arr1, arr2))\r\nprint(a)\r\n \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1 2]<br \/>\n[5 6]<br \/>\n[7 8]<br \/>\n[3 4]]<\/div>\n<h3>1. NumPy append() Function<\/h3>\n<p>The append()function is to create a new array along a specified axis. It creates a copy of the original array.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">np.append(arr, values, axis=None)\r\n<\/pre>\n<p>Array is the original array. The values are an array-like structure that appends to the original array. The axis defines the axis along which the array appends. Both the arrays will be flat if we don&#8217;t specify the axis.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr1 = np.array([[1, 2], [5, 6]])\r\narr2 = np.array([[7, 8], [3,4]])\r\na = np.append(arr1,arr2, axis=1)\r\nprint(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1 2 7 8]<br \/>\n[5 6 3 4]]<\/div>\n<h3>2. NumPy vstack() Function<\/h3>\n<p>We use the vstack()function to sequentially stack arrays in a vertical order i.e. along the rows. This is a more specific concept of array stacking. It is very useful for arrays with up to 3 dimensions. The vsplit() function splits the array into a list of multiple sub-arrays vertically.<\/p>\n<p>The vstack()then rebuilds these arrays split by the vsplit()function. Vertical stacking gives error when the number of columns is not equal in both the arrays.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = np.array([1, 2, 3])\r\nb = np.array([5,6,7])\r\nnp.vstack((a,b))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([[1, 2, 3],<br \/>\n[5, 6, 7]])<\/div>\n<h3>3. NumPy hstack() Function<\/h3>\n<p>We use this function to sequentially stack arrays in horizontal order i.e. along with the columns. This is a specific concept for column vice concatenation. We cannot perform horizontal stacking if the number of rows is not equal in both the arrays.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = np.array([[1, 2],[3,4]])\r\nb = np.array([[5,6],[7,8]])\r\nnp.hstack((a,b))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([[1, 2, 5, 6],<br \/>\n[3, 4, 7, 8]])<\/div>\n<h3>4. NumPy dstack() Function<\/h3>\n<p>This function is for stacking the arrays along depth. The resultant array is along a third new dimension.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = np.array([[1, 2],[3,4]])\r\nb = np.array([[5,6],[7,8]])\r\nnp.dstack((a,b))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([[[1, 5],<br \/>\n[2, 6]],[[3, 7],<br \/>\n[4, 8]]])<\/div>\n<h3>5. NumPy reshape() Function<\/h3>\n<p>Reshaping is for changing the shape of the array without changing the array data. When the shape of the array is not according to the requirements, we use this function. For unequal shaped arrays, we perform reshape prior to stacking.<\/p>\n<h4>a. Reshape with vertical stacking<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = np.array([[1,2],[3,4]])\r\nb = np.array([[1],[2]]).reshape(1,2) \r\nprint(b)\r\nnp.vstack((a,b))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1 2]]<br \/>\narray([[1, 2],<br \/>\n[3, 4],<br \/>\n[1, 2]])<\/div>\n<h4>b. Reshape with horizontal stacking<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a = np.array([[1,2],[3,4]])\r\nb = np.array((10,10)).reshape(2,1) \r\nprint(b)\r\nnp.hstack((a,b))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[10]<br \/>\n[10]]<br \/>\narray([[ 1, 2, 10],<br \/>\n[ 3, 4, 10]])<\/div>\n<h2>Summary<\/h2>\n<p>Stacking and joining functions in NumPy are very useful for giving new dimensions to an array. The stacking function along with the reshape function is to avoid unequal shape errors. Concatenate, stack, and append are general functions. The vertical, horizontal, and depth stacking are more specific.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stacking is the concept of joining arrays in NumPy. Arrays having the same dimensions can be stacked. The stacking is done along a new axis. Stacking leads to increased customization of arrays. We can&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79907,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[22793,22794,22792],"class_list":["post-79309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-joining-in-numpy","tag-stacking-and-joining-in-numpy","tag-stacking-in-numpy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stacking and Joining in NumPy - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about stacking and joining in Numpy. These are important functions for array in NumPy. Learn about dstack, hstack and vstack functions in NumPy.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stacking and Joining in NumPy - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about stacking and joining in Numpy. These are important functions for array in NumPy. Learn about dstack, hstack and vstack functions in NumPy.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-27T05:18:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Stacking-Joining-in-NumPy.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stacking and Joining in NumPy - DataFlair","description":"Learn about stacking and joining in Numpy. These are important functions for array in NumPy. Learn about dstack, hstack and vstack functions in NumPy.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/","og_locale":"en_US","og_type":"article","og_title":"Stacking and Joining in NumPy - DataFlair","og_description":"Learn about stacking and joining in Numpy. These are important functions for array in NumPy. Learn about dstack, hstack and vstack functions in NumPy.","og_url":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-27T05:18:05+00:00","article_modified_time":"2021-05-09T07:43:31+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Stacking-Joining-in-NumPy.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Stacking and Joining in NumPy","datePublished":"2020-07-27T05:18:05+00:00","dateModified":"2021-05-09T07:43:31+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/"},"wordCount":464,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Stacking-Joining-in-NumPy.jpg","keywords":["joining in numpy","Stacking and Joining in NumPy","stacking in numpy"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/","url":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/","name":"Stacking and Joining in NumPy - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Stacking-Joining-in-NumPy.jpg","datePublished":"2020-07-27T05:18:05+00:00","dateModified":"2021-05-09T07:43:31+00:00","description":"Learn about stacking and joining in Numpy. These are important functions for array in NumPy. Learn about dstack, hstack and vstack functions in NumPy.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Stacking-Joining-in-NumPy.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Stacking-Joining-in-NumPy.jpg","width":1200,"height":628,"caption":"Stacking & Joining in NumPy"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/stacking-and-joining-in-numpy\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"NumPy Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/numpy\/"},{"@type":"ListItem","position":3,"name":"Stacking and Joining in NumPy"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=79309"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79309\/revisions"}],"predecessor-version":[{"id":93061,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79309\/revisions\/93061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79907"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}