{"id":470213,"date":"2022-07-03T17:40:00","date_gmt":"2022-07-03T14:40:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-find-variance-using-python\/"},"modified":"2022-07-03T17:40:00","modified_gmt":"2022-07-03T14:40:00","slug":"how-to-find-variance-using-python","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/","title":{"rendered":"#How to find variance using Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a25b73038bbc\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" 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: #dd3333;color:#dd3333\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a25b73038bbc\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#%E2%80%9CHow_to_find_variance_using_Python%E2%80%9D\" >&#8220;How to find variance using Python&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#What_is_variance\" >What is variance?<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#The_best_experience_for_the_best_price\" >The best experience for the best price<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#How_do_you_find_the_variance\" >How do you find the variance?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#Why_use_n-1_when_calculating_the_sample_variance\" >Why use n-1 when calculating the sample variance?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#Calculating_variance_using_Python\" >Calculating variance using Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#Finding_variance_using_Pandas\" >Finding variance using Pandas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#Loading_example_data\" >Loading example data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#Finding_the_variance_for_the_BMW_data\" >Finding the variance for the BMW data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#Using_Pandas_var_function\" >Using Pandas var() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-find-variance-using-python\/#The_takeaway\" >The takeaway<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_find_variance_using_Python%E2%80%9D\"><\/span>&#8220;How to find variance using Python&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div id=\"article-main-content\">\n                            <em>This <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/data-science\/how-to-find-the-variance\">article<\/a> was originally published on <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/\">Built In<\/a> by Eric Kleppen.<\/em><\/p>\n<p>Variance is a powerful statistic used in data analysis and machine learning. It is one of the four main measures of variability along with range, interquartile range (<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/data-science\/how-to-find-outliers-with-iqr\">IQR<\/a>) and standard deviation. Understanding variance is important because it gives you insight into the spread of your data and can be used to compare differences in sample groups or identify important modeling features. Variance is also used in machine learning to understand changes in model performance due to using different samples of training data.<\/p>\n<p>Calculating variance is easy using Python. Before diving into the Python code, I\u2019ll first explain what variance is and how you can calculate it. By the end of this tutorial you\u2019ll have a better understanding of why variance is an important statistic, along with several methods for calculating it using Python.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_variance\"><\/span>What is variance?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"inarticle-wrapper channel-cta\">\n<div class=\"ica-text\">\n<h4><span class=\"ez-toc-section\" id=\"The_best_experience_for_the_best_price\"><\/span>The best experience for the best price<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Sign-up to our <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">news<\/a>letter and be the first to know when TNW Conference 2023 tickets are on sale!<\/p>\n<\/div>\n<\/div>\n<p>Variance is a statistic that measures dispersion. Low variance indicates that values are <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly similar and do not vary widely from the mean while high variance indicates values are more widely dispersed from the mean. You can use variance on either a sample set or the entire population as the calculation takes in all data points in the given set. Although the calculation differs slightly when you\u2019re looking at a sample versus population, you can calculate the variance as the average of the squared differences from the mean.<\/p>\n<p>Since the variance is a squared value, it can be difficult to interpret compared to other measures of variability like standard deviation. Regardless, reviewing variance can be helpful; doing so can make it easier for you to decide which <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/data-science\/t-test-vs-chi-square\">statistical tests<\/a> to use with your data. Depending on the statistical tests, uneven variance between samples could <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/data-science\/skewed-data\">skew<\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/data-science\/bias-variance-tradeoff\">bias<\/a> results.<\/p>\n<p>One of the popular <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/builtin.com\/data-science\/t-test-vs-chi-square\">statistical tests<\/a> that <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lies variance is called the analysis of variance (ANOVA) test. An ANOVA test is used to gauge whether any of the group means are significantly different from one another when analyzing a categorical independent variable and a quantitative dependent variable. For example, say you want to analyze whether <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">social media<\/a> use impacts hours of sleep. You could break social media use into different categories like low use, medium use and high use, then run an ANOVA test to gauge whether there are statistical differences between the group means. The test can show whether results are explained by group differences or individual differences.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_do_you_find_the_variance\"><\/span>How do you find the variance?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Calculating the variance for a data set can differ based on whether the set is the entire population or a sample of the population.<\/p>\n<p>The formula for calculating the variance of an entire population looks like this:<\/p>\n<p>\u03c3\u00b2 = \u2211 (X\u1d62\u2014 \u03bc)\u00b2 \/ N<\/p>\n<p>An explanation of the formula:<\/p>\n<ul>\n<li>\u03c3\u00b2 = population variance<\/li>\n<li>\u03a3 = sum of\u2026<\/li>\n<li>\u03a7\u1d62 = each value<\/li>\n<li>\u03bc = population mean<\/li>\n<li>\u039d = number of values in the population<\/li>\n<li>Using an example range of numbers, let\u2019s walk through the calculation step by step.<\/li>\n<\/ul>\n<p>Example range of numbers: 8, 6, 12, 3, 13, 9<\/p>\n<p>Find the population mean (\u03bc):<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388950 aligncenter js-lazy\" alt=\"Calculation for finding variance in Python\" width=\"1492\" height=\"274\" sizes=\"auto, (max-width: 1492px) 100vw, 1492px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM.png 1492w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-280x51.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-270x50.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-540x99.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-796x146.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388950 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM.png\" alt=\"Calculation for finding variance in Python\" width=\"1492\" height=\"274\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM.png 1492w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-280x51.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-270x50.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-540x99.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.02-PM-796x146.png 796w\"\/><\/noscript><\/figure>\n<p>Calculate deviations from the mean by subtracting the mean from each value.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388951 aligncenter js-lazy\" alt=\"Calculation for finding variance in Python\" width=\"1472\" height=\"534\" sizes=\"auto, (max-width: 1472px) 100vw, 1472px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM.png 1472w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-280x102.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-270x98.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-540x196.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-796x289.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388951 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM.png\" alt=\"Calculation for finding variance in Python\" width=\"1472\" height=\"534\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM.png 1472w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-280x102.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-270x98.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-540x196.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.29.12-PM-796x289.png 796w\"\/><\/noscript><\/figure>\n<p>Square each deviation to get a positive number.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388952 aligncenter js-lazy\" alt=\"Square each deviation with a positive number\" width=\"1484\" height=\"544\" sizes=\"auto, (max-width: 1484px) 100vw, 1484px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM.png 1484w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-280x103.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-270x99.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-540x198.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-796x292.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388952 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM.png\" alt=\"Square each deviation with a positive number\" width=\"1484\" height=\"544\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM.png 1484w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-280x103.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-270x99.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-540x198.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.11-PM-796x292.png 796w\"\/><\/noscript><\/figure>\n<p>Sum the squared values.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388953 aligncenter js-lazy\" alt=\"Sum the squared values\" width=\"1476\" height=\"180\" sizes=\"auto, (max-width: 1476px) 100vw, 1476px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM.png 1476w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-280x34.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-270x33.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-540x66.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-796x97.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388953 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM.png\" alt=\"Sum the squared values\" width=\"1476\" height=\"180\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM.png 1476w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-280x34.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-270x33.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-540x66.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.31.21-PM-796x97.png 796w\"\/><\/noscript><\/figure>\n<p>Divide the sum of squares by N or n-1.<\/p>\n<p>Since we\u2019re working with the entire population, we\u2019ll divide by N. If we were working with a sample of the population, we would divide by n-1.<\/p>\n<p>69.5\/6 = 11.583<\/p>\n<p>There we have it! The variance of our population is 11.583.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Why_use_n-1_when_calculating_the_sample_variance\"><\/span>Why use n-1 when calculating the sample variance?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Applying n-1 to the formula is called <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Bessel%27s_correction\">Bessel\u2019s correction<\/a>, named after Friedrich Bessel. When using samples, we need to calculate the estimated variance for the population. If we used N instead of n-1 for the sample, the estimate would be biased, potentially underestimating the population variance. Using n-1 will make the variance estimate larger, overestimating variability in samples, thus reducing biases.<\/p>\n<p>Let\u2019s recalculate the variance pretending the values are from a sample:<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388955 aligncenter js-lazy\" alt=\"recalculate the variance pretending the values are from a sample\" width=\"1464\" height=\"270\" sizes=\"auto, (max-width: 1464px) 100vw, 1464px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM.png 1464w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-280x52.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-270x50.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-540x100.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-796x147.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388955 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM.png\" alt=\"recalculate the variance pretending the values are from a sample\" width=\"1464\" height=\"270\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM.png 1464w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-280x52.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-270x50.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-540x100.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.35.48-PM-796x147.png 796w\"\/><\/noscript><\/figure>\n<p>As we can see, the variance is larger!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Calculating_variance_using_Python\"><\/span>Calculating variance using Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now that we\u2019ve done the calculation by hand, we can see that completing it for a large set of values would be very tedious. Luckily, Python can easily handle the calculation for very large data. We will explore two methods using Python:<\/p>\n<ul>\n<li>Write our own variance calculation function<\/li>\n<li>Use Pandas\u2019 built-in function<\/li>\n<\/ul>\n<p>Writing a variance function<\/p>\n<p>As we begin to write a function to calculation variance, think back to the steps we took when calculating by hand. We want the function to take in two parameters:<\/p>\n<ul>\n<li>population: an array of numbers<\/li>\n<li>is_sample: a Boolean to alter the calculation depending on whether we\u2019re working with a sample or population<\/li>\n<\/ul>\n<p>Start by defining the function that takes in the two parameters.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388957 size-full aligncenter js-lazy\" alt=\"Start by defining the function that takes in the two parameters.\" width=\"1490\" height=\"100\" sizes=\"auto, (max-width: 1490px) 100vw, 1490px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-280x19.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-270x18.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-540x36.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-796x53.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388957 size-full aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM.png\" alt=\"Start by defining the function that takes in the two parameters.\" width=\"1490\" height=\"100\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-280x19.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-270x18.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-540x36.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.26-PM-796x53.png 796w\"\/><\/noscript><\/figure>\n<p>Next, add logic to calculate the population mean.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388956 size-full aligncenter js-lazy\" alt=\"Next, add logic to calculate the population mean.\" width=\"1492\" height=\"138\" sizes=\"auto, (max-width: 1492px) 100vw, 1492px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM.png 1492w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-280x26.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-270x25.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-540x50.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-796x74.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388956 size-full aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM.png\" alt=\"Next, add logic to calculate the population mean.\" width=\"1492\" height=\"138\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM.png 1492w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-280x26.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-270x25.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-540x50.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.37.47-PM-796x74.png 796w\"\/><\/noscript><\/figure>\n<p>After calculating the mean, find the differences from the mean for each value. You can do this in one line using a list comprehension.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388959 size-full aligncenter js-lazy\" alt=\"find the differences from the mean for each value.\" width=\"1476\" height=\"134\" sizes=\"auto, (max-width: 1476px) 100vw, 1476px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM.png 1476w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-280x25.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-270x25.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-540x49.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-796x72.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388959 size-full aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM.png\" alt=\"find the differences from the mean for each value.\" width=\"1476\" height=\"134\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM.png 1476w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-280x25.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-270x25.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-540x49.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.39.59-PM-796x72.png 796w\"\/><\/noscript><\/figure>\n<p>Next, square the differences and sum them.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388958 aligncenter js-lazy\" alt=\"Next, square the differences and sum them.\" width=\"1490\" height=\"180\" sizes=\"auto, (max-width: 1490px) 100vw, 1490px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-280x34.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-270x33.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-540x65.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-796x96.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388958 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM.png\" alt=\"Next, square the differences and sum them.\" width=\"1490\" height=\"180\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-280x34.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-270x33.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-540x65.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.40.07-PM-796x96.png 796w\"\/><\/noscript><\/figure>\n<p>Lastly, calculate the variance. Using an If\/Else statement, we can utilize the is_sampleparameter. If is_sampleis true, calculate variance using (n-1). If it is false (the default), use N:<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388960 aligncenter js-lazy\" alt=\"Calculate the variance\" width=\"1496\" height=\"1258\" sizes=\"auto, (max-width: 1496px) 100vw, 1496px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM.png 1496w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-250x210.png 250w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-161x135.png 161w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-321x270.png 321w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-796x669.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388960 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM.png\" alt=\"Calculate the variance\" width=\"1496\" height=\"1258\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM.png 1496w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-250x210.png 250w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-161x135.png 161w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-321x270.png 321w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.44.45-PM-796x669.png 796w\"\/><\/noscript><\/figure>\n<p>We can test the calculation using the range of numbers we crunched by hand:<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388949 aligncenter js-lazy\" alt=\"How to find the variance in Python\" width=\"1192\" height=\"366\" sizes=\"auto, (max-width: 1192px) 100vw, 1192px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM.png 1192w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-280x86.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-270x83.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-540x166.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-796x244.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388949 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM.png\" alt=\"How to find the variance in Python\" width=\"1192\" height=\"366\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM.png 1192w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-280x86.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-270x83.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-540x166.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-01-at-3.24.52-PM-796x244.png 796w\"\/><\/noscript><\/figure>\n<p><iframe loading=\"lazy\" title=\"How To Calculate Variance\" width=\"500\" height=\"281\" srcdoc=\"&lt;style&gt;*{padding:0;margin:0;overflow:hidden}html,body{background:#000;height:100%}img{position:absolute;top:0;left:0;width:100%;height:100%;object-fit:cover;transition:opacity .1s cubic-bezier(0.4,0,1,1)}a:hover img+img{opacity:1!important}&lt;\/style&gt;&lt;a href=\" https:=\"\" src=\"https:\/\/img.youtube.com\/vi\/deIQeQzPK08\/hqdefault.jpg\" style=\"top: 50%;left:50%;width:68px;height:48px;transform:translate3d(-50%,-50%,0)\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><\/p>\n<p>\u00a0<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Finding_variance_using_Pandas\"><\/span>Finding variance using Pandas<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although we can write a function to calculate variance in less than 10 lines of code, there is an even easier way to find variance. You can do it in one line of code using Pandas. Let\u2019s load up some data and work through a real example of finding variance.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Loading_example_data\"><\/span>Loading example data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Pandas example uses the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.kaggle.com\/datasets\/danielkyrka\/bmw-pricing-challenge?resource=download\">BMW Price Challenge<\/a> data set from Kaggle, which is free to download. Begin by importing the Pandas library, and then reading the CSV file into a Pandas data frame:<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388990 aligncenter js-lazy\" alt=\"reading the CSV file into a Pandas data frame\" width=\"1490\" height=\"276\" sizes=\"auto, (max-width: 1490px) 100vw, 1490px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-280x52.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-270x50.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-540x100.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-796x147.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388990 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM.png\" alt=\"reading the CSV file into a Pandas data frame\" width=\"1490\" height=\"276\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-280x52.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-270x50.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-540x100.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.19.57-PM-796x147.png 796w\"\/><\/noscript><\/figure>\n<p>We can count the number of rows in the data set and display the first five rows to make sure everything loaded correctly:<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388991 aligncenter js-lazy\" alt=\"We can count the number of rows in the data set and display the first five rows to make sure everything loaded correctly:\" width=\"1490\" height=\"322\" sizes=\"auto, (max-width: 1490px) 100vw, 1490px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-280x61.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-270x58.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-540x117.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-796x172.png 796w\"\/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388991 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM.png\" alt=\"We can count the number of rows in the data set and display the first five rows to make sure everything loaded correctly:\" width=\"1490\" height=\"322\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM.png 1490w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-280x61.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-270x58.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-540x117.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.15-PM-796x172.png 796w\"\/><\/noscript><\/figure>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388992 size-full js-lazy\" alt=\"Displaying the first rows using bmw_df.head()\" width=\"1394\" height=\"388\" sizes=\"auto, (max-width: 1394px) 100vw, 1394px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM.png 1394w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-280x78.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-270x75.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-540x150.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-796x222.png 796w\"\/><figcaption><a rel=\"nofollow noopener\" target=\"_blank\" href=\"#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Feditorial.thenextweb.com%2Fcode-word%2F2022%2F07%2F03%2Fguide-find-variance-using-python%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Displaying the first rows using bmw_df.head()\" data-title=\"Share Displaying the first rows using bmw_df.head() on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Displaying the first rows using bmw_df.head() on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>Displaying the first rows using bmw_df.head()<\/figcaption><noscript><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-1388992 size-full\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM.png\" alt=\"Displaying the first rows using bmw_df.head()\" width=\"1394\" height=\"388\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM.png 1394w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-280x78.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-270x75.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-540x150.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.22.30-PM-796x222.png 796w\"\/><\/noscript><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Finding_the_variance_for_the_BMW_data\"><\/span>Finding the variance for the BMW data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Since the BMW data set is 4843 rows, calculating that by hand would\u2026not be fun. Instead we can simply plug in the column from the data frame into our calculate_variance function and return the variance. Let\u2019s find the variance for the numeric columns mileage, engine_power and price.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388993 js-lazy\" alt=\"Variance for numeric columns in the BMW data frame\" width=\"1206\" height=\"272\" sizes=\"auto, (max-width: 1206px) 100vw, 1206px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM.png 1206w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-280x63.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-270x61.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-540x122.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-796x180.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-1200x272.png 1200w\"\/><figcaption><a rel=\"nofollow noopener\" target=\"_blank\" href=\"#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Feditorial.thenextweb.com%2Fcode-word%2F2022%2F07%2F03%2Fguide-find-variance-using-python%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Variance for numeric columns in the BMW data frame\" data-title=\"Share Variance for numeric columns in the BMW data frame on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Variance for numeric columns in the BMW data frame on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>Variance for numeric columns in the BMW data frame<\/figcaption><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388993\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM.png\" alt=\"Variance for numeric columns in the BMW data frame\" width=\"1206\" height=\"272\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM.png 1206w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-280x63.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-270x61.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-540x122.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-796x180.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.24.37-PM-1200x272.png 1200w\"\/><\/noscript><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Using_Pandas_var_function\"><\/span>Using Pandas var() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In case we forget the calculation for variance and cannot write our own function, Pandas has a built-in function to calculate variance named var(). By default, it assumes a sample population and uses n-1 in the calculation; however, you can adjust the calculation by passing in the ddof=0 argument.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388994 js-lazy\" alt=\"Pandas var() function\" width=\"1560\" height=\"436\" sizes=\"auto, (max-width: 1560px) 100vw, 1560px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM.png 1560w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-280x78.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-270x75.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-540x151.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-1536x429.png 1536w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-796x222.png 796w\"\/><figcaption><a rel=\"nofollow noopener\" target=\"_blank\" href=\"#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Feditorial.thenextweb.com%2Fcode-word%2F2022%2F07%2F03%2Fguide-find-variance-using-python%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Pandas var() function\" data-title=\"Share Pandas var() function on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Pandas var() function on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>Pandas var() function<\/figcaption><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1388994\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM.png\" alt=\"Pandas var() function\" width=\"1560\" height=\"436\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM.png 1560w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-280x78.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-270x75.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-540x151.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-1536x429.png 1536w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/Screenshot-2022-07-03-at-4.26.02-PM-796x222.png 796w\"\/><\/noscript><\/figure>\n<p>As we can see the Var() function matches the values produced by our calculate_variance function, and it\u2019s only one line of code. Reviewing the results, we can see mileage has a high variance meaning the values tend to vary from the mean by a lot. That makes sense because many factors play into the distance a person needs to drive. By comparison, engine_power has a low variance which indicates the values don\u2019t vary widely from the mean.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_takeaway\"><\/span>The takeaway<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Understanding variance can be an important part of data analysis and machine learning because you can use it to assess group differences. Variance also impacts which statistical tests can help us make data driven decisions. High variance means values are greatly dispersed from the mean, while low variance means numbers are not widely dispersed from the mean. If we have a small set of values, it\u2019s possible to calculate the variance by hand in only five steps. For large data sets, we saw how simple it is to calculate variance using Python and Pandas. The Var() function in Pandas calculates the variance for the numerical columns in a data frame in only one line of code, which is pretty handy!\n                        <\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/news\/guide-find-variance-using-python\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to find variance using Python&#8221; This article was originally published on Built In by Eric Kleppen. Variance is a powerful statistic used in data analysis and machine learning. It is one of the four main measures of variability along with range, interquartile range (IQR) and standard deviation. Understanding variance is important because it gives&#8230;<\/p>\n","protected":false},"author":1,"featured_media":470214,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2022\/07\/sid-balachandran-_9a-3NO5KJE-unsplash.jpg&signature=feba2e17edd19b9046529dc877f1da71","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-470213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/470213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=470213"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/470213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/470214"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=470213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=470213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=470213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}