{"id":13204,"date":"2021-02-26T16:16:00","date_gmt":"2021-02-26T16:16:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=13204"},"modified":"2022-07-17T15:45:42","modified_gmt":"2022-07-17T15:45:42","slug":"statistics-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/statistics-module","title":{"rendered":"Python statistics module &#8211; 7 functions to know!"},"content":{"rendered":"\n<p>Python statistics module provides functions to calculate mathematical statistical data on a given set of numbers. It was introduced in Python 3.4 release. This is a very simple module and works on numbers &#8211; int, float, Decimal, and Fraction. In this article, we will be focusing on <strong>7 important functions of Python statistics module<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python statistics module functions<\/h2>\n\n\n\n<p>We would be focusing on some of the most prominent functions offered by statistics module in Python.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>mean() function<\/strong><\/li><li><strong>median() function<\/strong><\/li><li><strong>median_high() function<\/strong><\/li><li><strong>median_low() function<\/strong><\/li><li><strong>stdev() function<\/strong><\/li><li><strong>_sum() function<\/strong><\/li><li><strong>_counts() function<\/strong><\/li><\/ul>\n\n\n\n<p>Let&#8217;s have a look at them one by one.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. The mean() function<\/h2>\n\n\n\n<p><a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/examples\/mean-and-standard-deviation-python\">Mean<\/a> is one of the most used statistical measures to understand the data at a glance. The mean value represents the overall average estimation of the entire data at once. It&#8217;s calculated by adding all the values in the dataset and then dividing by the number of values.<\/p>\n\n\n\n<p>For example, if the dataset is [1,2,3,4,5], then the mean will be (1+2+3+4+5)\/5 = 3.<\/p>\n\n\n\n<p>The <code>statistics.mean()<\/code> function returns the mean from the set of numeric data values.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstatistics.mean(data)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. The median() function<\/h2>\n\n\n\n<p>Apart from the mean, we often come across situations where we need a value that represents the middle section of the entire data. With <code>statistics.median()<\/code> function, we can calculate the middle value for the data values. The median value is derived after sorting the dataset from the lowest to the greatest value. If the dataset has an even number of values, then the median is the average of the middle two numbers.<\/p>\n\n\n\n<p>For example, if the dataset is [1, 3, 10, 2], then first we will arrange it in the increasing order, i.e. [1, 2, 3, 10]. Since there is an even number of values, the median will be the average of the middle two numbers i.e. 2 and 3. So the median will be 2.5. For dataset [1, 10, 3], the median will be 3.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstatistics.median(data)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. The median_high() function<\/h2>\n\n\n\n<p>The <code>median_high()<\/code> function of the statistics module returns the higher median value from the dataset. The high median is especially useful when the data values are discrete in nature. If the dataset has an even number of values, the higher of the middle two values is returned. For an odd number of values, median_high is the same as the median value.<\/p>\n\n\n\n<p>For example, if the dataset is [1, 2, 3, 10], the median_high will be 3. If the dataset is [1, 3, 5], the median_high is the same as the median value 3.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstatistics.median_high(data)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. The statistics.median_low() function<\/h2>\n\n\n\n<p>The <code>median_low()<\/code> function returns the lowest of the median values from the set of values. It is useful when the data is discrete in nature and when we need the exact data point rather than interpolation points. If the dataset has an even number of values, the lower of the middle two values is returned. For an odd number of values, median_low is the same as the median value.<\/p>\n\n\n\n<p>For example, if the dataset is [1, 2, 3, 10], the median_low will be 2. If the dataset is [1, 3, 5], the median_low is the same as the median value 3.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstatistics.median_low(data)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. The statistics.stdev() function<\/h2>\n\n\n\n<p>The <code>stdev()<\/code> function returns the standard deviation of the data. First, the mean of data is calculated. Then the variation is calculated. The square root of the variance is the SD of the dataset.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstatistics.stdev(data)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. The _sum() function of statistics<\/h2>\n\n\n\n<p>When it comes to accumulation of the data points passed as arguments, the _sum() function comes into the picture. With <code>_sum()<\/code> function, we can get the summation of all the data values along with the count of all the data points passed to it.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstatistics._sum(data)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. The _counts() function<\/h2>\n\n\n\n<p>With <code>_counts()<\/code> function, we can get the frequency of every data point from the set of values. It counts the occurrence of every single data point and returns the list of tuples of size 2. The first value of the tuple is the dataset value and the second value is the occurrence count.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python statistics module functions examples<\/h2>\n\n\n\n<p>Let&#8217;s look at some examples of using the statistics module functions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport statistics\n\ndata = &#x5B;10, 203, 20, 30, 40, 50, 60, 70, 80, 100]\nres = statistics.mean(data)\nprint(&quot;Mean: &quot;, res)\n\nres = statistics.median(data)\nprint(&quot;Median: &quot;, res)\n\nres = statistics.median_high(data)\nprint(&quot;Median High value: &quot;, res)\n\nres = statistics.median_low(data)\nprint(&quot;Median Low value: &quot;, res)\n\nres = statistics.stdev(data)\nprint(&quot;Standard Deviation: &quot;, res)\n\nres = statistics._sum(data)\nprint(&quot;Sum: &quot;, res)\n\nres = statistics._counts(data)\nprint(&quot;Count: &quot;, res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMean:  66.3\nMedian:  55.0\nMedian High value:  60\nMedian Low value:  50\nStandard Deviation:  55.429735301150004\nSum:  (&lt;class &#039;int&#039;&gt;, Fraction(663, 1), 10)\nCount:  &#x5B;(10, 1), (203, 1), (20, 1), (30, 1), (40, 1), (50, 1), (60, 1), (70, 1), (80, 1), (100, 1)]    \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Python statistics module is useful to get the mean, median, mode, and standard deviation of the numerical datasets. They work on numbers and provide simple functions to calculate these values. However, if you are already using the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" data-type=\"post\" data-id=\"7694\">NumPy<\/a> or the <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" data-type=\"post\" data-id=\"2986\">Pandas<\/a> module, you can use their functions to calculate these values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/python-scipy\" data-type=\"post\" data-id=\"3248\">SciPy Module<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/mean-of-a-numpy-array\" data-type=\"post\" data-id=\"27802\">NumPy Mean<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/standard-deviation\">3 Ways to Calculate SD in Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/library\/statistics.html#module-statistics\" target=\"_blank\" rel=\"noopener\">Python.org Docs<\/a><\/li><li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.mean.html\" target=\"_blank\" rel=\"noopener\">numpy.org mean docs<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python statistics module provides functions to calculate mathematical statistical data on a given set of numbers. It was introduced in Python 3.4 release. This is a very simple module and works on numbers &#8211; int, float, Decimal, and Fraction. In this article, we will be focusing on 7 important functions of Python statistics module. Python [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":13226,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-13204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=13204"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13204\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/13226"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=13204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=13204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=13204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}