

{"id":145199,"date":"2025-06-02T12:19:21","date_gmt":"2025-06-02T06:49:21","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=145199"},"modified":"2025-06-02T12:19:21","modified_gmt":"2025-06-02T06:49:21","slug":"pandas-dataframe-groupby-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/","title":{"rendered":"Pandas DataFrame groupby() Method"},"content":{"rendered":"<h3>Program 1<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Advance Data Analysis \r\n#Group by method\r\nimport pandas as pd\r\nproduct=[('Limca',20,'Sanchi'),('Frooti',25,'Amul'),('Milk',20,'BK'),('Water',20,'BK'),\r\n       ('Limca',25,'AK'),('Frooti',20,'SK'),('Milk',28,'BK'),('Water',30,'Sanchi'),\r\n        ('Limca',27,'Sanchi'),('Frooti',29,'AK'),('Milk',25,'Amul'),('Water',45,'Sanchi'),\r\n        ('Water',29,'Amul'),('Limca',40,'DK'),('Frooti',32,'Amul'),('Water',40,'Sanchi')\r\n        ]\r\n#print(product)\r\ndf=pd.DataFrame(product,columns=['Product Name','Price','Distributor'])\r\n\r\ndf1=df.groupby('Product Name')\r\nprint(df1.agg([max]))\r\n#print(df1['Price'].agg([max,min]))\r\n#print(df1['Product Name'].agg(['count']))\r\n#print(df1['Price'].agg([sum]))\r\n# print(df1.get_group('Limca').max())\r\n# print(df1.get_group('Frooti').min())\r\n# df1=df.groupby('Distributor')\r\n# print(df1.get_group('Amul'))\r\n\r\n#df1=df.groupby('Product Name')\r\n#df1=df.groupby('Distributor')\r\n#df1=df.groupby('Price')\r\n# df1=df.groupby('Product Name')\r\n# for name ,rows in df1:\r\n#     print(name)\r\n#     print(rows)\r\n\r\n#print(type(df1))\r\n#print(df)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program 1 # Advance Data Analysis #Group by method import pandas as pd product=[(&#8216;Limca&#8217;,20,&#8217;Sanchi&#8217;),(&#8216;Frooti&#8217;,25,&#8217;Amul&#8217;),(&#8216;Milk&#8217;,20,&#8217;BK&#8217;),(&#8216;Water&#8217;,20,&#8217;BK&#8217;), (&#8216;Limca&#8217;,25,&#8217;AK&#8217;),(&#8216;Frooti&#8217;,20,&#8217;SK&#8217;),(&#8216;Milk&#8217;,28,&#8217;BK&#8217;),(&#8216;Water&#8217;,30,&#8217;Sanchi&#8217;), (&#8216;Limca&#8217;,27,&#8217;Sanchi&#8217;),(&#8216;Frooti&#8217;,29,&#8217;AK&#8217;),(&#8216;Milk&#8217;,25,&#8217;Amul&#8217;),(&#8216;Water&#8217;,45,&#8217;Sanchi&#8217;), (&#8216;Water&#8217;,29,&#8217;Amul&#8217;),(&#8216;Limca&#8217;,40,&#8217;DK&#8217;),(&#8216;Frooti&#8217;,32,&#8217;Amul&#8217;),(&#8216;Water&#8217;,40,&#8217;Sanchi&#8217;) ] #print(product) df=pd.DataFrame(product,columns=[&#8216;Product Name&#8217;,&#8217;Price&#8217;,&#8217;Distributor&#8217;]) df1=df.groupby(&#8216;Product Name&#8217;) print(df1.agg([max])) #print(df1[&#8216;Price&#8217;].agg([max,min])) #print(df1[&#8216;Product Name&#8217;].agg([&#8216;count&#8217;])) #print(df1[&#8216;Price&#8217;].agg([sum])) # print(df1.get_group(&#8216;Limca&#8217;).max()) # print(df1.get_group(&#8216;Frooti&#8217;).min()) # df1=df.groupby(&#8216;Distributor&#8217;)&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19475],"tags":[34301,9393,34303,34300,30038,34264,34302,9399],"class_list":["post-145199","post","type-post","status-publish","format-standard","hentry","category-pandas","tag-groupby-method-in-pandas","tag-pandas","tag-pandas-dataframe-groupby-method","tag-pandas-groupby-method","tag-pandas-practical","tag-pandas-program","tag-pandas-program-on-groupby-method","tag-pandas-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pandas DataFrame groupby() Method - DataFlair<\/title>\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\/pandas-dataframe-groupby-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pandas DataFrame groupby() Method - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Program 1 # Advance Data Analysis #Group by method import pandas as pd product=[(&#039;Limca&#039;,20,&#039;Sanchi&#039;),(&#039;Frooti&#039;,25,&#039;Amul&#039;),(&#039;Milk&#039;,20,&#039;BK&#039;),(&#039;Water&#039;,20,&#039;BK&#039;), (&#039;Limca&#039;,25,&#039;AK&#039;),(&#039;Frooti&#039;,20,&#039;SK&#039;),(&#039;Milk&#039;,28,&#039;BK&#039;),(&#039;Water&#039;,30,&#039;Sanchi&#039;), (&#039;Limca&#039;,27,&#039;Sanchi&#039;),(&#039;Frooti&#039;,29,&#039;AK&#039;),(&#039;Milk&#039;,25,&#039;Amul&#039;),(&#039;Water&#039;,45,&#039;Sanchi&#039;), (&#039;Water&#039;,29,&#039;Amul&#039;),(&#039;Limca&#039;,40,&#039;DK&#039;),(&#039;Frooti&#039;,32,&#039;Amul&#039;),(&#039;Water&#039;,40,&#039;Sanchi&#039;) ] #print(product) df=pd.DataFrame(product,columns=[&#039;Product Name&#039;,&#039;Price&#039;,&#039;Distributor&#039;]) df1=df.groupby(&#039;Product Name&#039;) print(df1.agg([max])) #print(df1[&#039;Price&#039;].agg([max,min])) #print(df1[&#039;Product Name&#039;].agg([&#039;count&#039;])) #print(df1[&#039;Price&#039;].agg([sum])) # print(df1.get_group(&#039;Limca&#039;).max()) # print(df1.get_group(&#039;Frooti&#039;).min()) # df1=df.groupby(&#039;Distributor&#039;)&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/\" \/>\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=\"2025-06-02T06:49:21+00:00\" \/>\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=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pandas DataFrame groupby() Method - DataFlair","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\/pandas-dataframe-groupby-method\/","og_locale":"en_US","og_type":"article","og_title":"Pandas DataFrame groupby() Method - DataFlair","og_description":"Program 1 # Advance Data Analysis #Group by method import pandas as pd product=[('Limca',20,'Sanchi'),('Frooti',25,'Amul'),('Milk',20,'BK'),('Water',20,'BK'), ('Limca',25,'AK'),('Frooti',20,'SK'),('Milk',28,'BK'),('Water',30,'Sanchi'), ('Limca',27,'Sanchi'),('Frooti',29,'AK'),('Milk',25,'Amul'),('Water',45,'Sanchi'), ('Water',29,'Amul'),('Limca',40,'DK'),('Frooti',32,'Amul'),('Water',40,'Sanchi') ] #print(product) df=pd.DataFrame(product,columns=['Product Name','Price','Distributor']) df1=df.groupby('Product Name') print(df1.agg([max])) #print(df1['Price'].agg([max,min])) #print(df1['Product Name'].agg(['count'])) #print(df1['Price'].agg([sum])) # print(df1.get_group('Limca').max()) # print(df1.get_group('Frooti').min()) # df1=df.groupby('Distributor')&#46;&#46;&#46;","og_url":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2025-06-02T06:49:21+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Pandas DataFrame groupby() Method","datePublished":"2025-06-02T06:49:21+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/"},"wordCount":7,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["groupby method in pandas","Pandas","pandas dataframe groupby method","pandas groupby method","pandas practical","pandas program","pandas program on groupby method","pandas tutorial"],"articleSection":["Pandas Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/","url":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/","name":"Pandas DataFrame groupby() Method - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2025-06-02T06:49:21+00:00","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/pandas-dataframe-groupby-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Pandas Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/pandas\/"},{"@type":"ListItem","position":3,"name":"Pandas DataFrame groupby() Method"}]},{"@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\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/145199","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\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=145199"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/145199\/revisions"}],"predecessor-version":[{"id":145209,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/145199\/revisions\/145209"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=145199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=145199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=145199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}