{"id":127848,"date":"2022-09-05T19:18:09","date_gmt":"2022-09-05T19:18:09","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=644227"},"modified":"2022-09-05T19:18:09","modified_gmt":"2022-09-05T19:18:09","slug":"python-finding-the-most-common-element-in-a-column","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/05\/python-finding-the-most-common-element-in-a-column\/","title":{"rendered":"Python \u2013 Finding the Most Common Element in a Column"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;644227&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">This article will show you how to find the most common element in a Pandas Column.<\/p>\n<p>To make it more interesting, we have the following running scenario:<\/p>\n<p><em>You have been provided with a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" target=\"_blank\">downloadable<\/a> CSV file containing crime statistics for the San Diego area, including their respective <a rel=\"noreferrer noopener\" href=\"https:\/\/secure.ssa.gov\/poms.nsf\/lnx\/0202613900\" data-type=\"URL\" data-id=\"https:\/\/secure.ssa.gov\/poms.nsf\/lnx\/0202613900\" target=\"_blank\">NCIC<\/a> Crime Codes. <\/em><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio\"\/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would you determine the most common <a rel=\"noreferrer noopener\" href=\"https:\/\/secure.ssa.gov\/poms.nsf\/lnx\/0202613900\" data-type=\"URL\" data-id=\"https:\/\/secure.ssa.gov\/poms.nsf\/lnx\/0202613900\" target=\"_blank\">NCIC<\/a> Crime Code that occurs in San Diego&#8217;s jurisdiction?<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li><strong>Method <\/strong>1: Use Pandas <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" target=\"_blank\"><code>mode()<\/code><\/a><\/li>\n<li><strong>Method <\/strong>2: Use Pandas <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a><\/li>\n<li><strong>Method <\/strong>3: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-methods-idxmax-idxmin-reindex-reindex_like-rename-rename_axis\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/pandas-dataframe-methods-idxmax-idxmin-reindex-reindex_like-rename-rename_axis\/\" target=\"_blank\"><code>idxmax()<\/code><\/a><\/li>\n<li><strong>Method 4<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.DataFrame.keys.html\" data-type=\"URL\" data-id=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.DataFrame.keys.html\" target=\"_blank\"><code>keys()<\/code><\/a><\/li>\n<li><strong>Method 5<\/strong>: Use Pandas <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-pandas-groupby-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-pandas-groupby-method\/\" target=\"_blank\"><code>groupby()<\/code><\/a><\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Preparation<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Before moving forward, please ensure the <a href=\"https:\/\/blog.finxter.com\/category\/pandas-library\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/category\/pandas-library\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pandas<\/a> library is installed. Click <a href=\"https:\/\/blog.finxter.com\/how-to-install-pandas-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-install-pandas-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> if you require instructions.<\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Then, add the following code to the top of each script. This snippet will allow the code in this article to run error-free.<\/p>\n<pre class=\"EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas as pd<\/pre>\n<p>After importing the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/category\/pandas-library\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/category\/pandas-library\/\" target=\"_blank\">Pandas<\/a> library, this library is referenced by calling the shortcode (<code>pd<\/code>).<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 1: Use Pandas mode()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" target=\"_blank\"><code>mode()<\/code><\/a> method to determine the single most common crime committed in San Diego on a given day.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr'])\nmax_crime = df['crimedescr'].mode()\nprint(max_crime)<\/pre>\n<p>The above code reads in the <code>crimedescr <\/code>column from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" target=\"_blank\">crimes.csv<\/a> file downloaded earlier. This saves to the DataFrame <code>df<\/code>.<\/p>\n<p>Next, the <code>crimedescr <\/code>column is then accessed, and the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" target=\"_blank\"><code>mode()<\/code><\/a> method is appended. This method returns a value or set of values that appear most often along a selected axis. The results save to <code>max_crime<\/code>.<\/p>\n<p>These results are output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>0 10851(A)VC TAKE VEH W\/O OWNER<br \/>Name: crimedescr, dtype: object<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>So, out of 7,854 rows of crimes committed on a given day for San Diego, the above offense was committed the highest number of times.<\/p>\n<p>The above code only provides us with the name of the most common crime; what if we need the crime name and the respective count?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2-3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'ucr_ncic_code'])\nmax_crime = df['crimedescr'].mode()\nmax_count = df['ucr_ncic_code'].mode() print(max_crime)\nprint(max_count)<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<p>The above code is output to the terminal and displays the following.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>0 <strong>10851(A)VC TAKE VEH W\/O OWNER<\/strong><br \/>Name: crimedescr, dtype: object<br \/>0 <strong>7000<\/strong><br \/>Name: ucr_ncic_code, dtype: int64<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Now, you are equipped to return to your boss and tell them that 7,000 offenses of <strong>10851 (A) VC TAKE VEH W\/O OWNER<\/strong> occurred on a given day in San Diego.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-finding-the-most-common-element-in-a-column\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-JKVy_HliQE%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 2: Use value_counts()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a> function to determine the top 5 most common crimes committed in San Diego on a given day.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'ucr_ncic_code'])\ntop5_names = df['crimedescr'].value_counts()[:5].index.tolist()\nprint(top5_names)<\/pre>\n<p>The above code reads in the <code>crimedescr<\/code> and <code>ucr_ncic_code <\/code>columns from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" target=\"_blank\">crimes.csv<\/a> file downloaded earlier. This saves to the DataFrame <code>df<\/code>.<\/p>\n<p>Then, the <code>crimedescr<\/code> column is accessed, and the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\">value_counts()<\/a><\/code> function is appended. This function returns a series containing the counts of unique values. <\/p>\n<p>However, since <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\">slicing<\/a> is also appended (<code>[:5]<\/code>), only the top five (5) common crimes are retrieved and then converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a>. The results save to <code>top5_names<\/code>.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['10851(A)VC TAKE VEH W\/O OWNER', 'TOWED\/STORED VEH-14602.6', '459 PC BURGLARY VEHICLE', 'TOWED\/STORED VEHICLE', '459 PC BURGLARY RESIDENCE']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>The above code only provides us with the names of the top 5 most common crimes; what if we need the names and their respective counts?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'ucr_ncic_code'])\ntop5 = df['crimedescr'].value_counts()[:5].sort_values(ascending=False)\nprint(top5)<\/pre>\n<p>The above output is sent to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>10851(A)VC TAKE VEH W\/O OWNER<\/td>\n<td>653<\/td>\n<\/tr>\n<tr>\n<td>TOWED\/STORED VEH-14602.6<\/td>\n<td>463<\/td>\n<\/tr>\n<tr>\n<td>459 PC BURGLARY VEHICLE<\/td>\n<td>462<\/td>\n<\/tr>\n<tr>\n<td>TOWED\/STORED VEHICLE<\/td>\n<td>434<\/td>\n<\/tr>\n<tr>\n<td>459 PC BURGLARY RESIDENCE<\/td>\n<td>356<\/td>\n<\/tr>\n<tr>\n<td>Name: crimedescr, dtype: int64<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-finding-the-most-common-element-in-a-column\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FD2ZueuWXST8%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<p>A cleaner way to achieve the same results is to use the following code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'ucr_ncic_code'])\ntop5 = df['crimedescr'].value_counts().nlargest(5)\nprint(top5)<\/pre>\n<p>The above code calls the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-nlargest-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/pandas-dataframe-nlargest-method\/\" target=\"_blank\"><code>nlargest()<\/code><\/a> method to determine and retrieve the top five (5) common crimes. The output is identical to the above.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>10851(A)VC TAKE VEH W\/O OWNER<\/td>\n<td>653<\/td>\n<\/tr>\n<tr>\n<td>TOWED\/STORED VEH-14602.6<\/td>\n<td>463<\/td>\n<\/tr>\n<tr>\n<td>459 PC BURGLARY VEHICLE<\/td>\n<td>462<\/td>\n<\/tr>\n<tr>\n<td>TOWED\/STORED VEHICLE<\/td>\n<td>434<\/td>\n<\/tr>\n<tr>\n<td>459 PC BURGLARY RESIDENCE<\/td>\n<td>356<\/td>\n<\/tr>\n<tr>\n<td>Name: crimedescr, dtype: int64<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>A much cleaner and more precise output to send to the boss!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 3: Use value_counts() and idxmax()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-methods-idxmax-idxmin-reindex-reindex_like-rename-rename_axis\/\" target=\"_blank\"><code>idxmax()<\/code><\/a> to determine the single most common crime committed in San Diego on a given day.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'ucr_ncic_code'])\nmax_crime = df['crimedescr'].value_counts().idxmax()\nprint(max_crime)<\/pre>\n<p>The above code reads in the <code>crimedescr<\/code> and <code>ucr_ncic_code <\/code>columns from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" target=\"_blank\">crimes.csv<\/a> file downloaded earlier. This saves to the DataFrame <code>df<\/code>.<\/p>\n<p>Then, the <code>crimedescr <\/code>column is accessed, and the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/pandas-dataframe-mode-method\/\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a> function is appended. This function returns a series containing the count of unique values.<\/p>\n<p>Next, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-methods-idxmax-idxmin-reindex-reindex_like-rename-rename_axis\/\" target=\"_blank\"><code>idxmax()<\/code><\/a> is appended. This method returns the index of the first occurrence of the maximum index(es) over a selected axis. <\/p>\n<p>The results save to <code>max_crime<\/code> and are output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>10851(A)VC TAKE VEH W\/O OWNER<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 4: Use value_counts() and keys()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-count-occurrences-of-elements-in-pandas\/\" target=\"_blank\"><code>value_counts()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-methods-idxmax-idxmin-reindex-reindex_like-rename-rename_axis\/\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.DataFrame.keys.html\" target=\"_blank\"><code>keys()<\/code><\/a> to determine the top 5 most common crimes committed in unique grid areas of San Diego on a given day.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'grid', 'ucr_ncic_code'])\ntop5_grids = df['grid'].value_counts().keys()[:5]\nprint(top5_grids)<\/pre>\n<p>The above code reads in the <code>crimedescr<\/code>, <code>grid<\/code>, and the <code>ucr_ncic_code <\/code>columns from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" target=\"_blank\">crimes.csv<\/a> file downloaded earlier. This saves to the DataFrame <code>df<\/code>.<\/p>\n<p>Let&#8217;s break the highlighted line down. <\/p>\n<p>If <code>df['grid'].value_counts()<\/code> was output to the terminal, the following would display (snippet). However, we have added a heading row to make it more understandable, and only five (5) rows are displayed.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Grid #<\/strong><\/td>\n<td><strong>Grid Total<\/strong><\/td>\n<\/tr>\n<tr>\n<td>742<\/td>\n<td>115<\/td>\n<\/tr>\n<tr>\n<td>969<\/td>\n<td>105<\/td>\n<\/tr>\n<tr>\n<td>958 <\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>564<\/td>\n<td>80<\/td>\n<\/tr>\n<tr>\n<td>1084<\/td>\n<td>71<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Next, the code <code>keys()[:5]<\/code> is appended. The final output displays as follows.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>Int64Index([742, 969, 958, 564, 1084], dtype='int64')<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 5: Use groupby()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This examples uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-pandas-groupby-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-pandas-groupby-method\/\" target=\"_blank\"><code>groupby()<\/code><\/a> to group our data on the Crime Code and displays the totals in descending order.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv('crimes.csv', usecols=['crimedescr', 'ucr_ncic_code']) res = (df.groupby(['ucr_ncic_code','crimedescr']).size() .sort_values(ascending=False) .reset_index(name='count'))\nprint(res)<\/pre>\n<p>The above code reads in the <code>crimedescr<\/code> and the <code>ucr_ncic_code <\/code>columns from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/02\/crimes.csv\" target=\"_blank\">crimes.csv<\/a> file downloaded earlier. This saves to the DataFrame <code>df<\/code>.<\/p>\n<p>Next, the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-pandas-groupby-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-pandas-groupby-method\/\" target=\"_blank\"><code>groupby()<\/code><\/a> function is called and passed the first argument: <code>df.groupby(['ucr_ncic_code','crimedescr']).size()<\/code>. If this was output to the terminal at this point, the following would display (snippet).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(df.groupby(['ucr_ncic_code','crimedescr']).size())<\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>ucr_ncic_code<\/td>\n<td>crimedescr<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>909 <\/td>\n<td><\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>999<\/td>\n<td><\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>197<\/td>\n<td><\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>664<\/td>\n<td><\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1099<\/td>\n<td><\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>As you can see, the other arguments need to be added to turn this into something usable. Sorting the data in descending order and adding a count column will provide the results we are looking for. <\/p>\n<p>If the original Method 5 code example was output to the terminal, the following would display.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>ucr_ncic_code<\/td>\n<td>crimedescr<\/td>\n<td>count<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>2404<\/td>\n<td>10851(A)VC TAKE VEH W\/O OWNER<\/td>\n<td>653<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>7000<\/td>\n<td>TOWED\/STORED VEH-14602.6<\/td>\n<td>463<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2299<\/td>\n<td>459 PC BURGLARY VEHICLE<\/td>\n<td>462<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>7000<\/td>\n<td>TOWED\/STORED VEHICLE<\/td>\n<td>434<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>2204<\/td>\n<td>459 PC BURGLARY RESIDENCE<\/td>\n<td>356<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-finding-the-most-common-element-in-a-column\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FgUk4hMCuu2g%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Summary<\/h2>\n<p>This article has provided five (5) ways to find the most common element in a Panda Column. These examples should provide you with enough information to select the one that best meets your coding requirements.<\/p>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Programming Humor &#8211; Python<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"588\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-65.png\" alt=\"\" class=\"wp-image-471102\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-65.png 518w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-65-264x300.png 264w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><figcaption><em>&#8220;I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I&#8217;m leaving you.&#8221;<\/em> &#8212; <a rel=\"noreferrer noopener\" href=\"https:\/\/imgs.xkcd.com\/comics\/python.png\" data-type=\"URL\" data-id=\"https:\/\/imgs.xkcd.com\/comics\/python.png\" target=\"_blank\">xkcd<\/a><\/figcaption><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview This article will show you how to find the most common element in a Pandas Column. To make it more interesting, we have the following running scenario: You have been provided with a downloadable CSV file containing crime statistics for the San Diego area, including their [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-127848","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127848"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127848\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}