{"id":8154,"date":"2025-05-13T07:37:15","date_gmt":"2025-05-13T07:37:15","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=8154"},"modified":"2025-05-20T07:09:04","modified_gmt":"2025-05-20T07:09:04","slug":"how-to-create-pivot-tables-in-python-pandas","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/","title":{"rendered":"How to Create Pivot Tables in Python Pandas"},"content":{"rendered":"\n<p>Pivot tables provide a great way to summarize and analyze data during data processing. It is very useful way to work with large datasets. Earlier it was available only in Microsoft Excel but its utility makes it required in every data processing software. Sometimes Python developers need to create pivot tables using pandas dataframe. In this article, we will learn how to create pivot tables in Python pandas.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#What_are_Pivot_Tables\" >What are Pivot Tables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#How_to_Create_Pivot_Tables_in_Python_Pandas\" >How to Create Pivot Tables in Python Pandas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#1_Create_Dataframe\" >1. Create Dataframe<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#2_Create_Pivot_Table\" >2. Create Pivot Table<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#Pivot_Table_Using_Multiple_Indexes\" >Pivot Table Using Multiple Indexes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#Pivot_Table_using_multiple_aggregation\" >Pivot Table using multiple aggregation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#Pivot_Table_on_filtered_data\" >Pivot Table on filtered data<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_Pivot_Tables\"><\/span>What are Pivot Tables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Pivot table is a useful tool to quickly summarize your data by organizing, summarizing and filtering it. It is generally used to group your data to form aggregations. You can also calculate many statistical metrics such as average, mean, median, etc. Pivot tables are commonly used to spot outliers and trends. Earlier, they were available mainly in MS Excel but now they are available in most <a href=\"https:\/\/ubiq.co\/dashboard-builder\">dashboard building tools<\/a>, as well as programming languages. It is mainly used if you have a large table and want to quickly understand its summary such as min, max, average, mean values, for each group of column values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Create_Pivot_Tables_in_Python_Pandas\"><\/span>How to Create Pivot Tables in Python Pandas<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the steps to create <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pivot_table\" target=\"_blank\" rel=\"noreferrer noopener\">pivot tables<\/a> in Python pandas. You can do this using pivot_table() function available for all dataframes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Create_Dataframe\"><\/span>1. Create Dataframe<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the commands to create a dataframe in Python Pandas.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br><br>data = {'Category': ['A', 'A', 'B', 'B', 'A', 'B'],<br>        'Product': ['X', 'Y', 'X', 'Y', 'X', 'X'],<br>        'Amount': [10, 12, 15, 18, 20, 14]}<br>df = pd.DataFrame(data)<br>print(df)<br><br>## output<br><br>  Category Product  Amount<br>0        A       X      10<br>1        A       Y      12<br>2        B       X      15<br>3        B       Y      18<br>4        A       X      20<br>5        B       X      14<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Create_Pivot_Table\"><\/span>2. Create Pivot Table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Next, we create pivot table using pivot_table() function. It takes three main arguments &#8211; index, values and aggfunc. Index means the columns to use as new row index. Values means the columns to aggregate. And aggfunc means the aggregation function such as sum, count, etc. Here is its complete syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DataFrame.pivot_table( values=None, index=None, columns=None, aggfunc=\u2019mean\u2019, fill_value=None, margins=False, dropna=True) <\/pre>\n\n\n\n<p>Here is an example to pivot table by aggregating Amount column for each Product column value, using sum function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pivot = df.pivot_table(index=['Product'],values=['Amount'],aggfunc='sum')<br>print(pivot)<br><br>## output<br>        <br>Product  Amount       <br>X            59<br>Y            30<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pivot_Table_Using_Multiple_Indexes\"><\/span>Pivot Table Using Multiple Indexes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also aggregate data by both category and product, by simply mentioning those column names in indexes argument as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pivot = df.pivot_table(index=['Category','Product'],<br>\t\t\t\t\tvalues=['Amount'],<br>\t\t\t\t\taggfunc='sum')<br>print(pivot)<br><br>## output<br><br>Category Product        <br>A        X            30<br>         Y            12<br>B        X            29<br>         Y            18<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pivot_Table_using_multiple_aggregation\"><\/span>Pivot Table using multiple aggregation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also use multiple aggregation functions to aggregate your data, by mentioning the function names one after the other in a comma-separated manner, in aggfunc argument.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pivot = df.pivot_table(index=['Category','Product'],<br>\t\t\t\t\tvalues=['Amount'],<br>\t\t\t\t\taggfunc={'median', 'mean', 'min'})<br>print(pivot)<br><br>## output<br><br>                   mean median min<br>Category Product                  <br>A        X         15.0   15.0  10<br>         Y         12.0   12.0  12<br>B        X         14.5   14.5  14<br>         Y         18.0   18.0  18<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pivot_Table_on_filtered_data\"><\/span>Pivot Table on filtered data<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also filter the dataframe before creating a pivot table out of it. Here is an example where we filter rows where amount&gt;10 and then use the result to create pivot table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pivot = df[<strong>df['Amount'] &gt; 10<\/strong>].pivot_table(index=['Category','Product'],<br>\t\t\t\t\tvalues=['Amount'],<br>\t\t\t\t\taggfunc={'median', 'mean', 'min'})<br>print(pivot)<br><br>## output<br><br>                   mean median min<br>Category Product                  <br>A        X         20.0   20.0  20<br>         Y         12.0   12.0  12<br>B        X         14.5   14.5  14<br>         Y         18.0   18.0  18<\/pre>\n\n\n\n<p>After creation of pivot tables, if you need to find any duplicate column values or rows in the result, then you can do so using duplicated() function.<\/p>\n\n\n\n<p>Once you have created pivot tables, you can also <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-pandas-dataframe-to-excel-spreadsheets\/\">export it to excel spreadsheet<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to create Pivot table in Pandas. We have also learnt several use cases for creating pivot tables. You can customize it as per your requirement. Pivot tables are super useful for statistical analysis, data processing and reporting. They allow you to easily summarize data, understand trends, identify patterns and outliers. Being able to create pivot tables in Python pandas is a very helpful as it strengthens data processing capabilities of your Python script.<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/\">How to Connect Pandas to Database<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/top-python-libraries-for-machine-learning\/\">Top Python Learning Libraries in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/machine-learning-life-cycle\/\">Machine Learning LifeCycle<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to create pivor tables in Python Pandas. Here are the different ways to do this.<\/p>\n","protected":false},"author":1,"featured_media":8176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[422],"class_list":["post-8154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-pivot-tables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create Pivot Tables in Python Pandas - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes you may need to create pivot tables in Python Pandas. Here are the different ways to do this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Pivot Tables in Python Pandas - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes you may need to create pivot tables in Python Pandas. Here are the different ways to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-13T07:37:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T07:09:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"260\" \/>\n\t<meta property=\"og:image:height\" content=\"173\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Create Pivot Tables in Python Pandas\",\"datePublished\":\"2025-05-13T07:37:15+00:00\",\"dateModified\":\"2025-05-20T07:09:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/\"},\"wordCount\":537,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1\",\"keywords\":[\"pivot tables\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/\",\"name\":\"How to Create Pivot Tables in Python Pandas - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1\",\"datePublished\":\"2025-05-13T07:37:15+00:00\",\"dateModified\":\"2025-05-20T07:09:04+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes you may need to create pivot tables in Python Pandas. Here are the different ways to do this.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1\",\"width\":260,\"height\":173,\"caption\":\"create pivot table in python pandas\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-create-pivot-tables-in-python-pandas\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Pivot Tables in Python Pandas\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Pivot Tables in Python Pandas - Ubiq BI","description":"Sometimes you may need to create pivot tables in Python Pandas. Here are the different ways to do this.","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:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Pivot Tables in Python Pandas - Ubiq BI","og_description":"Sometimes you may need to create pivot tables in Python Pandas. Here are the different ways to do this.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-05-13T07:37:15+00:00","article_modified_time":"2025-05-20T07:09:04+00:00","og_image":[{"width":260,"height":173,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Create Pivot Tables in Python Pandas","datePublished":"2025-05-13T07:37:15+00:00","dateModified":"2025-05-20T07:09:04+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/"},"wordCount":537,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1","keywords":["pivot tables"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/","name":"How to Create Pivot Tables in Python Pandas - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1","datePublished":"2025-05-13T07:37:15+00:00","dateModified":"2025-05-20T07:09:04+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes you may need to create pivot tables in Python Pandas. Here are the different ways to do this.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1","width":260,"height":173,"caption":"create pivot table in python pandas"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Pivot Tables in Python Pandas"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/create-pivot-table-python-pandas.jpg?fit=260%2C173&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-27w","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=8154"}],"version-history":[{"count":26,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8154\/revisions"}],"predecessor-version":[{"id":8321,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8154\/revisions\/8321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/8176"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=8154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=8154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=8154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}