{"id":139,"date":"2026-03-02T15:32:02","date_gmt":"2026-03-02T10:32:02","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=139"},"modified":"2026-03-17T07:59:59","modified_gmt":"2026-03-17T02:59:59","slug":"groupby-and-aggregation","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/","title":{"rendered":"GroupBy and Aggregation"},"content":{"rendered":"\n<p>GroupBy and Aggregation are powerful features in pandas used to summarize and analyze data by categories.<\/p>\n\n\n\n<p>They help you answer questions like:<\/p>\n\n\n\n<p>What is the total sales per department?<br>What is the average salary by job role?<br>How many employees are in each city?<\/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<p>Create a sample dataset:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = {<br>    \"Department\": [\"IT\", \"HR\", \"IT\", \"Finance\", \"HR\", \"Finance\"],<br>    \"Employee\": [\"Ali\", \"Sara\", \"Ahmed\", \"John\", \"Ayesha\", \"David\"],<br>    \"Salary\": [60000, 50000, 65000, 70000, 52000, 75000]<br>}df = pd.DataFrame(data)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What is GroupBy?<\/h2>\n\n\n\n<p>GroupBy splits the data into groups based on one or more columns.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"ColumnName\")<\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"Department\")<\/pre>\n\n\n\n<p>This groups data by Department.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Aggregation Functions<\/h2>\n\n\n\n<p>Aggregation means performing calculations on grouped data.<\/p>\n\n\n\n<p>Common aggregation functions:<\/p>\n\n\n\n<p>sum()<br>mean()<br>count()<br>min()<br>max()<br>std()<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Total Salary by Department<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"Department\")[\"Salary\"].sum()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Average Salary by Department<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"Department\")[\"Salary\"].mean()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 3: Count Employees per Department<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"Department\")[\"Employee\"].count()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Aggregations<\/h2>\n\n\n\n<p>You can apply multiple functions at once:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"Department\")[\"Salary\"].agg([\"sum\", \"mean\", \"max\"])<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">GroupBy with Multiple Columns<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby([\"Department\", \"Employee\"])[\"Salary\"].sum()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Resetting Index<\/h2>\n\n\n\n<p>Grouped results often create a new index.<br>To convert it back to a normal DataFrame:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.groupby(\"Department\")[\"Salary\"].sum().reset_index()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Example in Data Analytics<\/h2>\n\n\n\n<p>GroupBy is used to:<\/p>\n\n\n\n<p>Analyze sales by region<br>Calculate revenue by product<br>Find average marks by class<br>Summarize expenses by category<br>Measure performance by department<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>GroupBy splits data into categories, and Aggregation performs calculations on those groups.<\/p>\n\n\n\n<p>Together, they are essential tools for summarizing and analyzing structured data in real-world analytics projects.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773716505805\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\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 > GroupBy and Aggregation<\/span><\/span><\/div>","protected":false},"menu_order":73,"template":"","class_list":["post-139","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>GroupBy and Aggregation - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Pandas GroupBy and aggregation to summarize data, calculate totals, averages, and analyze datasets efficiently.\" \/>\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\/groupby-and-aggregation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"GroupBy and Aggregation - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Pandas GroupBy and aggregation to summarize data, calculate totals, averages, and analyze datasets efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T02:59:59+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/groupby-and-aggregation\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/groupby-and-aggregation\\\/\",\"name\":\"GroupBy and Aggregation - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T10:32:02+00:00\",\"dateModified\":\"2026-03-17T02:59:59+00:00\",\"description\":\"Learn Pandas GroupBy and aggregation to summarize data, calculate totals, averages, and analyze datasets efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/groupby-and-aggregation\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/groupby-and-aggregation\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/groupby-and-aggregation\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR DATA ANALYTICS (PYDA) > Pandas > GroupBy and Aggregation\"}]},{\"@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":"GroupBy and Aggregation - One Language. Endless Possibilities","description":"Learn Pandas GroupBy and aggregation to summarize data, calculate totals, averages, and analyze datasets efficiently.","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\/groupby-and-aggregation\/","og_locale":"en_US","og_type":"article","og_title":"GroupBy and Aggregation - One Language. Endless Possibilities","og_description":"Learn Pandas GroupBy and aggregation to summarize data, calculate totals, averages, and analyze datasets efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T02:59:59+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/","url":"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/","name":"GroupBy and Aggregation - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T10:32:02+00:00","dateModified":"2026-03-17T02:59:59+00:00","description":"Learn Pandas GroupBy and aggregation to summarize data, calculate totals, averages, and analyze datasets efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/groupby-and-aggregation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR DATA ANALYTICS (PYDA) > Pandas > GroupBy and Aggregation"}]},{"@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\/139","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=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}