Signup/Sign In
PUBLISHED ON: MARCH 19, 2021

Pandas DataFrame median() Method

In this tutorial, we will learn the Python pandas DataFrame.median() method. This method can be used to get median of the values over the requested axis. It returns Series and if the level is specified, it returns the DataFrame.

The below shows the syntax of the DataFrame.median() method.

Syntax

DataFrame.median(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Parameters

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, function applied over the index axis and when the axis=1 function applied over the column axis.

skipna: bool(True or False). The default value is None. If this parameter is True, it excludes all NA/null values when computing the result.

level: It represents the int or level name, the default value is None. It counts along with a particular level, if the DataFrame is a Multi index, collapsing into a Series.

numeric_only: bool(True or False), the default is None. If this parameter is True, it includes only float, int, boolean columns.

**kwargs: Additional keyword arguments to be passed to the function.

Example: Getting the median value using the DataFrame.median() Method

Let's create a DataFrame and get the median value over the index axis by assigning parameter axis=0 in the DataFrame.median() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,52,78],"B":[77,45,96],"C":[16,23,135],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.median(axis=0))


------The DataFrame is------
A B C D
0 0 77 16 17
1 52 45 23 22
2 78 96 135 56
---------------------------
A 52.0
B 77.0
C 23.0
D 22.0
dtype: float64

Example: Getting the median value using the DataFrame.median() Method

Let's create a DataFrame and get the median value over the column axis by assigning parameter axis=1 in the DataFrame.median() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,52,78],"B":[77,45,96],"C":[16,23,135],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.median(axis=1))


------The DataFrame is------
A B C D
0 0 77 16 17
1 52 45 23 22
2 78 96 135 56
---------------------------
0 16.5
1 34.0
2 87.0
dtype: float64

Example: Getting the median value using the DataFrame.median() Method

Let's create a DataFrame with null values and get the median value over the index axis excluding null values by passing parameter skipna=False in the DataFrame.median() method. It includes all NA/null values when computing the results. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,None,78],"B":[77,45,None],"C":[16,23,None],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.median(axis=0,skipna=False))


------The DataFrame is------
A B C D
0 0.0 77.0 16.0 17
1 NaN 45.0 23.0 22
2 78.0 NaN NaN 56
---------------------------
A NaN
B NaN
C NaN
D 22.0
dtype: float64

Example: Getting the median value using the DataFrame.median() Method

Here, we are creating a DataFrame with null values and get the median value over the index axis excluding null values by passing parameter skipna=True in the DataFrame.median() method. It excludes all NA/null values when computing the results. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,None,78],"B":[77,45,None],"C":[16,23,None],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.median(axis=0,skipna=True))

Once we run the program we will get the following output.


------The DataFrame is------
A B C D
0 0.0 77.0 16.0 17
1 NaN 45.0 23.0 22
2 78.0 NaN NaN 56
---------------------------
A 39.0
B 61.0
C 19.5
D 22.0
dtype: float64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.median() method. We learned the syntax, parameters and applied this method on the DataFrame to understand the DataFrame.median() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.