{"id":558,"date":"2024-04-03T20:41:44","date_gmt":"2024-04-03T18:41:44","guid":{"rendered":"https:\/\/bitwise.exposed\/?p=558"},"modified":"2026-03-16T09:38:47","modified_gmt":"2026-03-16T08:38:47","slug":"dealing-with-outliers","status":"publish","type":"post","link":"https:\/\/bitwise.exposed\/index.php\/2024\/04\/03\/dealing-with-outliers\/","title":{"rendered":"Dealing with outliers &#8211; IQR method"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-border-secondary-color\">Cookbook | <em>Data Cleaning<\/em><\/mark><\/h4>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\" style=\"grid-template-columns:15% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"536\" src=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/github_icon_01.png\" alt=\"\" class=\"wp-image-618 size-full\" srcset=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/github_icon_01.png 544w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/github_icon_01-300x296.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">Published and available on GitHub as part of the Python Cookbook Repository: <em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\"><a href=\"https:\/\/github.com\/tabularo\/PythonCookbook\/blob\/master\/Cleaning\/outliers\/outliners_IQR_method.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">outliners_IQR_method.ipynb<\/a><\/mark><\/em><\/p>\n<\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Dealing with <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">outliers <\/mark>in numerical columns &#8211; using <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">IQR method<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Interquartile Range (IQR) method is a statistical technique used to identify outliers in a dataset. The IQR is the range between the first quartile (25th percentile) and the third quartile (75th percentile) of the data. It represents the range within which the middle 50% of the values lie.<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><strong>Key steps in the IQR method for outlier detection:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">Calculate the Q1 and Q3:<\/mark> Q1 and Q3 (the 25th and 75th percentile, respectively) divide the ordered dataset into four equal parts. Q1 is the middle number between the smallest number and the median. Q3 is the middle value between the median and the highest value.<br><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">Calculate the IQR:<\/mark> The IQR is calculated as the difference between Q3 and Q1. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">IQR = Q3 &#8211; Q1<\/mark><br><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">Identify potential outliers:<\/mark> Any data point that is <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">below Q1 &#8211; 1.5*IQR<\/mark> or <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">above Q3 + 1.5*IQR<\/mark> is considered as an outlier. The factor of 1.5 defines the &#8222;whiskers&#8220; of a box plot, this could be used to visualize, for understanding dispersion and skewness in the data.<br><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">In the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">default setting<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">outliers <\/mark>are expected to <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">fall above or below these whiskers<\/mark>. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">Adjusting <\/mark>the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">multiplier <\/mark>(1.5) <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">allows for more or less flexibility in accounting for outliers<\/mark>, based on the specific data and use case.<br><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">In other words, the IQR is a way of understanding the spread of the middle 50% of your data, and the method as a whole is a way of detecting and handling outliers<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Rough visualization:<\/strong><br>We are interested in all values between the lower and upper whisker line, values outside would be outliers for us.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"595\" src=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/image-1.png\" alt=\"\" class=\"wp-image-561\" srcset=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/image-1.png 831w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/image-1-300x215.png 300w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/image-1-768x550.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Example<\/h5>\n\n\n\n<h6 class=\"wp-block-heading\">Create Demo Data<\/h6>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import pandas as pd\nimport numpy as np\n\n# Sample data with some outliers\ndata = {\n    'Age': [25, 30, 35, 40, 45, 500],  # 500 is an outlier\n    'Income': [50000, 70000, 80000, 90000, 100000, 9999999]  # 9999999 is an outlier\n}\ndf = pd.DataFrame(data)\n\nprint(&quot;Original DataFrame:&quot;)\nprint(df)\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">import<\/span><span style=\"color: #000000\"> pandas <\/span><span style=\"color: #AF00DB\">as<\/span><span style=\"color: #000000\"> pd<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">import<\/span><span style=\"color: #000000\"> numpy <\/span><span style=\"color: #AF00DB\">as<\/span><span style=\"color: #000000\"> np<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Sample data with some outliers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">data = {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #A31515\">&#39;Age&#39;<\/span><span style=\"color: #000000\">: [<\/span><span style=\"color: #098658\">25<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">30<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">35<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">40<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">45<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">500<\/span><span style=\"color: #000000\">],  <\/span><span style=\"color: #008000\"># 500 is an outlier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #A31515\">&#39;Income&#39;<\/span><span style=\"color: #000000\">: [<\/span><span style=\"color: #098658\">50000<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">70000<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">80000<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">90000<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">100000<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">9999999<\/span><span style=\"color: #000000\">]  <\/span><span style=\"color: #008000\"># 9999999 is an outlier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">df = pd.DataFrame(data)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Original DataFrame:&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(df)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Original DataFrame:\n\n   Age   Income\n&quot;&quot;&quot; 12.5  35'000  Lower Whisker limit in this sample data &quot;&quot;&quot;\n0   25    50000\n1   30    70000\n2   35    80000\n3   40    90000\n4   45   100000\n&quot;&quot;&quot; 62.5 135'000  Upper Whisker limit in this sample data &quot;&quot;&quot;\n5  500  9999999\n\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># Original DataFrame:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">   Age   Income<\/span><\/span>\n<span class=\"line\"><span style=\"color: #A31515\">&quot;&quot;&quot; 12.5  35&#39;000  Lower Whisker limit in this sample data &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">25<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #098658\">50000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">30<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #098658\">70000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">35<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #098658\">80000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">40<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #098658\">90000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">4<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">45<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">100000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #A31515\">&quot;&quot;&quot; 62.5 135&#39;000  Upper Whisker limit in this sample data &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #098658\">500<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #098658\">9999999<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h6 class=\"wp-block-heading\">Calculate Whisker<\/h6>\n\n\n\n<p class=\"wp-block-paragraph\">This step is just for demo or adjustment.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Function to calculate lower and upper whiskers of a given dataset\ndef calculate_whiskers(data, multiplier=1.5):\n    # Quartiles\n    Q1 = np.percentile(data, 25)\n    Q3 = np.percentile(data, 75)\n\n    # InterQuartile Range\n    IQR = Q3 - Q1\n\n    # Whiskers\n    lower_whisker = Q1 - multiplier * IQR\n    upper_whisker = Q3 + multiplier * IQR\n\n    return lower_whisker, upper_whisker\n\n\n# Get lower and upper whiskers for 'Age' and 'Income'\nage_lower, age_upper = calculate_whiskers(df['Age'])\nincome_lower, income_upper = calculate_whiskers(df['Income'])\n\nprint(&quot;Age - Lower Whisker: &quot;, age_lower)\nprint(&quot;Age - Upper Whisker: &quot;, age_upper)\nprint(&quot;Income - Lower Whisker: &quot;, income_lower)\nprint(&quot;Income - Upper Whisker: &quot;, income_upper)\n\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># Function to calculate lower and upper whiskers of a given dataset<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">def<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">calculate_whiskers<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">data<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">multiplier<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #098658\">1.5<\/span><span style=\"color: #000000\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\"># Quartiles<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    Q1 = np.percentile(data, <\/span><span style=\"color: #098658\">25<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    Q3 = np.percentile(data, <\/span><span style=\"color: #098658\">75<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\"># InterQuartile Range<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    IQR = Q3 - Q1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\"># Whiskers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    lower_whisker = Q1 - multiplier * IQR<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    upper_whisker = Q3 + multiplier * IQR<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> lower_whisker, upper_whisker<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Get lower and upper whiskers for &#39;Age&#39; and &#39;Income&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">age_lower, age_upper = calculate_whiskers(df[<\/span><span style=\"color: #A31515\">&#39;Age&#39;<\/span><span style=\"color: #000000\">])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">income_lower, income_upper = calculate_whiskers(df[<\/span><span style=\"color: #A31515\">&#39;Income&#39;<\/span><span style=\"color: #000000\">])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Age - Lower Whisker: &quot;<\/span><span style=\"color: #000000\">, age_lower)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Age - Upper Whisker: &quot;<\/span><span style=\"color: #000000\">, age_upper)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Income - Lower Whisker: &quot;<\/span><span style=\"color: #000000\">, income_lower)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Income - Upper Whisker: &quot;<\/span><span style=\"color: #000000\">, income_upper)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"Age - Lower Whisker:  12.5\nAge - Upper Whisker:  62.5\nIncome - Lower Whisker:  35000.0\nIncome - Upper Whisker:  135000.0\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #000000\">Age - Lower Whisker:  <\/span><span style=\"color: #098658\">12.5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Age - Upper Whisker:  <\/span><span style=\"color: #098658\">62.5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Income - Lower Whisker:  <\/span><span style=\"color: #098658\">35000.0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Income - Upper Whisker:  <\/span><span style=\"color: #098658\">135000.0<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Clean outliers from data:<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Now we want to exclude the outliers<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Function to clean data\ndef clean_data(df, multiplier=1.5):\n    # Dealing with outliers in numerical columns - using IQR method\n    Q1 = df.quantile(0.25)\n    Q3 = df.quantile(0.75)\n    IQR = Q3 - Q1\n    df = df[~((df &lt; (Q1 - multiplier * IQR)) | (df &gt; (Q3 + multiplier * IQR))).any(axis=1)]\n\n    return df\n\n\n# Apply the function on DataFrame\ncleaned_df = clean_data(df)\nprint(&quot;\\nCleaned DataFrame:&quot;)\nprint(cleaned_df)\n\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># Function to clean data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">def<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">clean_data<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">df<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">multiplier<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #098658\">1.5<\/span><span style=\"color: #000000\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\"># Dealing with outliers in numerical columns - using IQR method<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    Q1 = df.quantile(<\/span><span style=\"color: #098658\">0.25<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    Q3 = df.quantile(<\/span><span style=\"color: #098658\">0.75<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    IQR = Q3 - Q1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    df = df[~((df &lt; (Q1 - multiplier * IQR)) | (df &gt; (Q3 + multiplier * IQR))).any(<\/span><span style=\"color: #001080\">axis<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">)]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> df<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Apply the function on DataFrame<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">cleaned_df = clean_data(df)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">Cleaned DataFrame:&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">(cleaned_df)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Cleaned DataFrame:\n\n   Age  Income\n0   25   50000\n1   30   70000\n2   35   80000\n3   40   90000\n4   45  100000\n\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># Cleaned DataFrame:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">   Age  Income<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">25<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">50000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">30<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">70000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">35<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">80000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">40<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">90000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #098658\">4<\/span><span style=\"color: #000000\">   <\/span><span style=\"color: #098658\">45<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #098658\">100000<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Cookbook | Data Cleaning Published and available on GitHub as part of the Python Cookbook Repository: outliners_IQR_method.ipynb Dealing with outliers in numerical columns &#8211; using IQR method The Interquartile Range (IQR) method is a statistical technique used to identify outliers in a dataset. The IQR is the range between the first quartile (25th percentile) and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":566,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,28,13,27],"tags":[],"class_list":["post-558","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-cleaning","category-data-engineering","category-python","category-statistics"],"_links":{"self":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/comments?post=558"}],"version-history":[{"count":16,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/558\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/558\/revisions\/623"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media\/566"}],"wp:attachment":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media?parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/categories?post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/tags?post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}