{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/count-the-unique-values-in-a-column-in-a-pandas-dataframe/",
    "result": {"data":{"markdownRemark":{"html":"<div class=\"table-of-contents\">\n<ul>\n<li><a href=\"#value_counts\">value_counts()</a></li>\n<li><a href=\"#unique\">unique()</a></li>\n<li><a href=\"#nunique\">nunique()</a></li>\n</ul>\n</div>\n<h3 id=\"value_counts\" style=\"position:relative;\"><a href=\"#value_counts\" aria-label=\"value_counts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>value_counts()</h3>\n<p>We can return counts of unique values in a column using the <del>value_counts()</del> method.</p>\n<p>Below, we have read an Excel file into a DataFrame.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">import</span><span class=\"mtk15\"> pandas </span><span class=\"mtk10\">as</span><span class=\"mtk15\"> pd</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> pd.</span><span class=\"mtk6\">read_excel</span><span class=\"mtk15\">(</span><span class=\"mtk16\">&quot;budget.xlsx&quot;</span><span class=\"mtk15\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"4\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"5\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 549px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9774b3e073eb7e9b96aac7650a7a201d/ffc28/budget.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 70.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAB70lEQVQoz12R2W7cMBAE/f8/Z8CBF0gCr0SKp6QVb3J4ScEe9tqZ90ZNdb80b6tRx7631owx3vvW+nHsKcYYgrU2pmSMUUqFELz3SitjtHOulPJy3C7nLIQ4n8/vp3fKGCXT7z9/OefjMJyHAWP89vbrdDpNZDoPw/nj4/X1lXHxCJdSQgi9d0qZc55SggntvQdvE8BxHMtllVLu+7HffmSUGWOfZOdcihEjFGLgnCMyAcBlnZ0PpZRlXYUQOWcASDGN46iUepJjjDlnSmnOWWtFKd17N3oLMe29b5viQrbWaq2ttWmatNbPsHMuxognrLUWnE2ELMtCCfEhllLmZWGM38kAwAhV6jNcSw0h9t6FlErpcRw45ymlbV0T5GPflVJSzr331lpvjfOfzsYYAEAIG2MRxiNGJZftsljnaymXdb0b3ckY4W37dK61AkCthRCaAFJKXIjr8mqLAMe+b9sm5wf56oyx1uYb2VpICY3IWisFp4QCwLYu1vtSyvVt8XT+v+0QQq11nu/OI2Gs937bJe0PZ/mdbL7I124uGwBQSr33WukR4ZzzF3lZZinnOznGhBB6OgOAtbbWKoRwzjEyMS5yLuqyxvRw5kJ8kQkh+gd5U9cBGXPeS8E551IIRmmIt53nRQj5IIeIr4Xpf9FEGVfIxG0SAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/9774b3e073eb7e9b96aac7650a7a201d/ffc28/budget.png\"\n        srcset=\"/static/9774b3e073eb7e9b96aac7650a7a201d/56d15/budget.png 200w,\n/static/9774b3e073eb7e9b96aac7650a7a201d/d9f49/budget.png 400w,\n/static/9774b3e073eb7e9b96aac7650a7a201d/ffc28/budget.png 549w\"\n        sizes=\"(max-width: 549px) 100vw, 549px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Let's count the unique values in the <del>LTL Flag</del> column:</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[</span><span class=\"mtk16\">&quot;LTL Flag&quot;</span><span class=\"mtk15\">].</span><span class=\"mtk6\">value_counts</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 308px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/cb4fa20d764fca7908fce7cc995280aa/9e38b/valueCounts.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 21.999999999999996%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAm0lEQVQI13WO2wqDQBBD/f/vs7IKUi/1glvXpS3VnZ2ZFKWUUmjIS5KHk4SWaxhamvroLBM3Te29U4WwAPDer9uGP0q+g4rkJi/P5WQnO1oAy+K2QACIiIVFRUVijERRoQlU3wYgUpgiM+a5bmM/zrPt+jZLU+dc011OWToMw+N+q6sqM3kgSn6eiAgzK6CHdmYIzHzAo8refKYXzsvmrApa10IAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Unique Value Counts\"\n        title=\"Unique Value Counts\"\n        src=\"/static/cb4fa20d764fca7908fce7cc995280aa/9e38b/valueCounts.png\"\n        srcset=\"/static/cb4fa20d764fca7908fce7cc995280aa/56d15/valueCounts.png 200w,\n/static/cb4fa20d764fca7908fce7cc995280aa/9e38b/valueCounts.png 308w\"\n        sizes=\"(max-width: 308px) 100vw, 308px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h3 id=\"unique\" style=\"position:relative;\"><a href=\"#unique\" aria-label=\"unique permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>unique()</h3>\n<p>If we want to find out the unique values (<em>including NaN</em>) in a column without the count, we can use the <del>unique()</del> method.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[</span><span class=\"mtk16\">&quot;LTL Flag&quot;</span><span class=\"mtk15\">].</span><span class=\"mtk6\">unique</span><span class=\"mtk15\">()</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk6\">array</span><span class=\"mtk15\">([nan, </span><span class=\"mtk16\">&#39;LTL&#39;</span><span class=\"mtk15\">, </span><span class=\"mtk16\">&#39;NON-LTL&#39;</span><span class=\"mtk15\">], </span><span class=\"mtk8 mtki\">dtype</span><span class=\"mtk12\">=</span><span class=\"mtk9\">object</span><span class=\"mtk15\">)</span></span></span></code></pre>\n<h3 id=\"nunique\" style=\"position:relative;\"><a href=\"#nunique\" aria-label=\"nunique permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>nunique()</h3>\n<p>We can find out the total count of unique values in a column using the <del>nunique()</del> method. <del>nunique()</del> by default, does not count null values. So, we pass <del>dropna = False</del> to count the null values as well.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[</span><span class=\"mtk16\">&quot;LTL Flag&quot;</span><span class=\"mtk15\">].</span><span class=\"mtk6\">nunique</span><span class=\"mtk15\">(</span><span class=\"mtk8 mtki\">dropna</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk5\">False</span><span class=\"mtk15\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk5\">3</span></span></span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .synthwave-84 { background-color: #262335; }\n  .synthwave-84 .mtki { font-style: italic; }\n  .synthwave-84 .mtk10 { color: #FEDE5D; }\n  .synthwave-84 .mtk15 { color: #FF7EDBFF; }\n  .synthwave-84 .mtk12 { color: #FFFFFFEE; }\n  .synthwave-84 .mtk6 { color: #36F9F6; }\n  .synthwave-84 .mtk16 { color: #FF8B39; }\n  .synthwave-84 .mtk8 { color: #72F1B8; }\n  .synthwave-84 .mtk9 { color: #FE4450; }\n  .synthwave-84 .mtk5 { color: #F97E72; }\n  .synthwave-84 .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(255, 255, 255, 0.1));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(255, 255, 255, 0.5));\n  }\n</style>","frontmatter":{"title":"Count the Unique Values in a Column in a Pandas DataFrame","date":"2021-08-08"}}},"pageContext":{"slug":"/count-the-unique-values-in-a-column-in-a-pandas-dataframe/","prev":{"fields":{"slug":"/memory-optimization-in-pandas-dataframes/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/filter-a-pandas-dataframe-based-on-a-condition/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}