Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)

10 Feb, 2020
Python has the ability to manipulate some statistical data and calculate results of various statistical operations using the file “statistics“, useful in domain of mathematics.
Important Average and measure of central location functions :
1. mean() :- This function returns the mean or average of the data passed in its arguments. If passed argument is empty, StatisticsError is raised.
2. mode() :- This function returns the number with maximum number of occurrences. If passed argument is empty, StatisticsError is raised.
# Python code to demonstrate the working of# mean() and mode()  # importing statistics to handle statistical operationsimport statistics  # initializing listli = [1, 2, 3, 3, 2, 2, 2, 1]  # using mean() to calculate average of list elementsprint ("The average of list values is : ",end="")print (statistics.mean(li))  # using mode() to print maximum occurring of list elementsprint ("The maximum occurring element is  : ",end="")print (statistics.mode(li))
Output:
The average of list values is : 2.0
The maximum occurring element is  : 2

 
3. median() :- This function is used to calculate the median, i.e middle element of data. If passed argument is empty, StatisticsError is raised.
4. median_low() :- This function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the lower of two middle elements. If passed argument is empty, StatisticsError is raised.
5. median_high() :- This function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the higher of two middle elements. If passed argument is empty, StatisticsError is raised.
# Python code to demonstrate the working of# median(), median_low() and median_high()  # importing statistics to handle statistical operationsimport statistics  # initializing listli = [1, 2, 2, 3, 3, 3]  # using median() to print median of list elementsprint ("The median of list element is : ",end="")print (statistics.median(li))  # using median_low() to print low median of list elementsprint ("The lower median of list element is : ",end="")print (statistics.median_low(li))  # using median_high() to print high median of list elementsprint ("The higher median of list element is : ",end="")print (statistics.median_high(li))
Output:
The median of list element is : 2.5
The lower median of list element is : 2
The higher median of list element is : 3

 
6. median_grouped() :- This function is used to compute group median, i.e 50th percentile of the data. If passed argument is empty, StatisticsError is raised.
# Python code to demonstrate the working of# median_grouped()  # importing statistics to handle statistical operationsimport statistics  # initializing listli = [1, 2, 2, 3, 3, 3]  # using median_grouped() to calculate 50th percentileprint ("The 50th percentile of data is : ",end="")print (statistics.median_grouped(li))
Output:
The 50th percentile of data is : 2.5

Python Programming Tutorial | Statistical Functions - Part 1 | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersPython Programming Tutorial | Statistical Functions - Part 1 | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:46•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=CIWbQ7_nRj4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Statistical Functions in Python | Set 2 ( Measure of Spread)
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
statistical-algorithms
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts