Python | Pandas dataframe.median()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas dataframe.median() function return the median of the values for the requested axis
If the method is applied on a pandas series object, then the method returns a scalar value which is the median value of all the observations in the dataframe. If the method is applied on a pandas dataframe object, then the method returns a pandas series object which contains the median of the values over the specified axis.

Syntax:DataFrame.median(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
Parameters :
axis : Align object with threshold along the given axis.
skipna : Exclude NA/null values when computing the result
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
numeric_only : Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.



Returns : median : Series or DataFrame (if level specified)

Example #1: Use median() function to find the median of all the observations over the index axis.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the dataframe 
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],
                   "B":[5, 2, 54, 3, 2],
                   "C":[20, 16, 7, 3, 8], 
                   "D":[14, 3, 17, 2, 6]})
  
# Print the dataframe
df

chevron_right


Lets use the dataframe.median() function to find the median over the index axis

filter_none

edit
close

play_arrow

link
brightness_4
code

# Find median Even if we do not specify axis = 0, the method 
# will return the median over the index axis by default
df.median(axis = 0)

chevron_right


Output :

 
Example #2: Use median() function on a dataframe which has Na values. Also find the median over the column axis.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the dataframe 
df = pd.DataFrame({"A":[12, 4, 5, None, 1],
                   "B":[7, 2, 54, 3, None], 
                   "C":[20, 16, 11, 3, 8], 
                   "D":[14, 3, None, 2, 6]})
  
# Print the dataframe
df

chevron_right


Lets implement the median function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# skip the Na values while finding the median
df.median(axis = 1, skipna = True)

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.