{"id":140,"date":"2026-03-02T15:35:31","date_gmt":"2026-03-02T10:35:31","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=140"},"modified":"2026-03-17T08:02:09","modified_gmt":"2026-03-17T03:02:09","slug":"merging-and-joining","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/","title":{"rendered":"Merging and Joining"},"content":{"rendered":"\n<p>Merging and Joining are used to combine multiple datasets into one.<br>In real-world Data Analytics projects, data is often stored in different tables, so combining them is an essential skill.<\/p>\n\n\n\n<p>First, import pandas:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why Merging is Important<\/h2>\n\n\n\n<p>You may have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One table with employee details<\/li>\n\n\n\n<li>Another table with salary information<\/li>\n\n\n\n<li>A third table with department data<\/li>\n<\/ul>\n\n\n\n<p>To perform complete analysis, you must combine them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Data<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Employee Table<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">employees = pd.DataFrame({<br>    \"Emp_ID\": [1, 2, 3, 4],<br>    \"Name\": [\"Ali\", \"Sara\", \"Ahmed\", \"Ayesha\"],<br>    \"Department\": [\"IT\", \"HR\", \"Finance\", \"IT\"]<br>})<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Salary Table<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">salaries = pd.DataFrame({<br>    \"Emp_ID\": [1, 2, 3, 5],<br>    \"Salary\": [60000, 50000, 70000, 45000]<br>})<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1. Merge in Pandas<\/h2>\n\n\n\n<p>The <code>merge()<\/code> function is similar to SQL JOIN.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.merge(left_df, right_df, on=\"column_name\", how=\"type\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Merge<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Inner Join<\/h3>\n\n\n\n<p>Returns only matching records from both tables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.merge(employees, salaries, on=\"Emp_ID\", how=\"inner\")<\/pre>\n\n\n\n<p>Only Emp_ID present in both tables will appear.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Left Join<\/h3>\n\n\n\n<p>Returns all records from the left table and matching from the right.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.merge(employees, salaries, on=\"Emp_ID\", how=\"left\")<\/pre>\n\n\n\n<p>All employees will appear. Missing salaries will show NaN.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Right Join<\/h3>\n\n\n\n<p>Returns all records from the right table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.merge(employees, salaries, on=\"Emp_ID\", how=\"right\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Outer Join<\/h3>\n\n\n\n<p>Returns all records from both tables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.merge(employees, salaries, on=\"Emp_ID\", how=\"outer\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Joining Using join()<\/h2>\n\n\n\n<p><code>join()<\/code> is mainly used to join on index.<\/p>\n\n\n\n<p>Set index first:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">employees.set_index(\"Emp_ID\", inplace=True)<br>salaries.set_index(\"Emp_ID\", inplace=True)<\/pre>\n\n\n\n<p>Join:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">employees.join(salaries, how=\"left\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Concatenation (Stacking Data)<\/h2>\n\n\n\n<p>Concatenate rows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.concat([employees, employees])<\/pre>\n\n\n\n<p>Concatenate columns:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pd.concat([employees, salaries], axis=1)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When to Use What?<\/h2>\n\n\n\n<p>Use <code>merge()<\/code> when joining based on a common column.<br>Use <code>join()<\/code> when joining based on index.<br>Use <code>concat()<\/code> when stacking data vertically or horizontally.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Merging and Joining are Important<\/h2>\n\n\n\n<p>In Data Analytics, you often:<\/p>\n\n\n\n<p>Combine sales data with customer data<br>Merge employee records with payroll data<br>Join product data with inventory records<br>Combine multiple monthly reports<\/p>\n\n\n\n<p>Understanding merging helps you build complete and accurate datasets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Merging and Joining allow you to combine multiple datasets into one meaningful dataset.<br>Mastering these techniques is essential for real-world Data Analytics projects.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR DATA ANALYTICS (PYDA) > Pandas > Merging and Joining<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773716632054\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":74,"template":"","class_list":["post-140","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Merging and Joining - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Pandas merge, join, and concat to combine datasets, perform joins, and integrate multiple tables for data analysis.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merging and Joining - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Pandas merge, join, and concat to combine datasets, perform joins, and integrate multiple tables for data analysis.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T03:02:09+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/merging-and-joining\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/merging-and-joining\\\/\",\"name\":\"Merging and Joining - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T10:35:31+00:00\",\"dateModified\":\"2026-03-17T03:02:09+00:00\",\"description\":\"Learn Pandas merge, join, and concat to combine datasets, perform joins, and integrate multiple tables for data analysis.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/merging-and-joining\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/merging-and-joining\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/merging-and-joining\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR DATA ANALYTICS (PYDA) > Pandas > Merging and Joining\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Merging and Joining - One Language. Endless Possibilities","description":"Learn Pandas merge, join, and concat to combine datasets, perform joins, and integrate multiple tables for data analysis.","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:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/","og_locale":"en_US","og_type":"article","og_title":"Merging and Joining - One Language. Endless Possibilities","og_description":"Learn Pandas merge, join, and concat to combine datasets, perform joins, and integrate multiple tables for data analysis.","og_url":"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T03:02:09+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/","url":"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/","name":"Merging and Joining - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T10:35:31+00:00","dateModified":"2026-03-17T03:02:09+00:00","description":"Learn Pandas merge, join, and concat to combine datasets, perform joins, and integrate multiple tables for data analysis.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/merging-and-joining\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR DATA ANALYTICS (PYDA) > Pandas > Merging and Joining"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}