Pandas DataFrame DataFrame.median() function
Python Pandas DataFrame.median() function calculates the median of the elements of the DataFrame object along the specified axis.
The median is not the average, but the middle value of a list of numbers.
pandas.DataFrame.median()
grammar
DataFrame.median(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
parameter
axis |
Find the median along a row ( axis=0 ) or column ( )axis=1 |
skipna |
Boolean. Exclude NaN value( skipna=True ) or include NaN value( skipna=False ) |
level |
If axis is MultiIndex , then find the median along a specific level |
numeric_only |
Boolean. Boolean. For numeric_only=True , only include float , int , and boolean columns |
**kwargs |
Additional keyword arguments to functions |
Return Value
If not specified level
, returns the median along the requested axis Series
, otherwise returns the median DataFrame
.
Example Code: DataFrame.median()
Method to find the median along the column axis
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9]})
print("DataFrame:")
print(df)
medians=df.median()
print("medians of Each Column:")
print(medians)
Output:
DataFrame:
X Y
0 1 4
1 2 3
2 7 8
3 5 2
4 10 9
medians of Each Column:
X 5.0
Y 4.0
dtype: float64
Computes the median of two columns, X
and Y
, and returns a object containing the median of each column Series
.
In Pandas, to find the median of a column in a DataFrame, we just call median()
the median function on that column.
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9]})
print("DataFrame:")
print(df)
medians=df["X"].median()
print("medians of Each Column:")
print(medians)
Output:
DataFrame:
X Y
0 1 4
1 2 3
2 7 8
3 5 2
4 10 9
medians of Each Column:
5.0
It just gives the median of the columns DataFrame
in .X
Example Code: DataFrame.median()
Method to find the median along the row axis
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9],
'Z': [2, 7, 6, 10, 5]})
print("DataFrame:")
print(df)
medians=df.median(axis=1)
print("medians of Each Row:")
print(medians)
Output:
DataFrame:
X Y Z
0 1 4 2
1 2 3 7
2 7 8 6
3 5 2 10
4 10 9 5
medians of Each Row:
0 2.0
1 3.0
2 7.0
3 5.0
4 9.0
dtype: float64
It calculates the median of all rows and returns a Series
object containing the median for each row.
In Pandas, to find DataFrame
the median of a row in , we just call median()
the function to calculate the median of that row.
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9],
'Z': [2, 7, 6, 10, 5]})
print("DataFrame:")
print(df)
median=df.iloc[[0]].median(axis=1)
print("median of 1st Row:")
print(median)
Output:
DataFrame:
X Y Z
0 1 4 2
1 2 3 7
2 7 8 6
3 5 2 10
4 10 9 5
median of 1st Row:
0 2.0
dtype: float64
It only gives DataFrame
the median of the first row of values in .
We use iloc
the method to select a row based on its index.
Example Code: DataFrame.median()
Method Ignoring NaN
Values to Find the Median
We use skipna
the default value of the parameter, that is , find the median of along the specified axis skipna=True
by ignoring the value of .NaN
DataFrame
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 7, None, 10, 8],
'Y': [None, 3, 8, 2, 9, 6],
'Z': [2, 7, 6, 10, None, 5]})
print("DataFrame:")
print(df)
median=df.median(skipna=True)
print("medians of Each Row:")
print(median)
Output:
DataFrame:
X Y Z
0 1.0 NaN 2.0
1 2.0 3.0 7.0
2 7.0 8.0 6.0
3 NaN 2.0 10.0
4 10.0 9.0 NaN
5 8.0 6.0 5.0
medians of Each Row:
X 7.0
Y 6.0
Z 6.0
dtype: float64
If we set skipna=True
, it ignores the in DataFrame NaN
. It allows us NaN
to calculate DataFrame
the median along the column axis by ignoring the values.
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 7, None, 10],
'Y': [5, 3, 8, 2, 9],
'Z': [2, 7, 6, 10, 4]})
print("DataFrame:")
print(df)
median=df.median(skipna=False)
print("medians of Each Row:")
print(median)
Output:
DataFrame:
X Y Z
0 1.0 5 2
1 2.0 3 7
2 7.0 8 6
3 NaN 2 10
4 10.0 9 4
medians of Each Row:
X NaN
Y 5.0
Z 6.0
dtype: float64
Here, we get NaN
the value as X
the median of the column because X
there are values in the column NaN
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Pandas to CSV without index
Publish Date:2025/05/01 Views:159 Category:Python
-
As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates
Convert Pandas DataFrame to Dictionary
Publish Date:2025/05/01 Views:197 Category:Python
-
This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan
Convert Pandas DataFrame columns to lists
Publish Date:2025/05/01 Views:191 Category:Python
-
When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m
Subtracting Two Columns in Pandas DataFrame
Publish Date:2025/05/01 Views:120 Category:Python
-
Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi
Dropping columns by index in Pandas DataFrame
Publish Date:2025/05/01 Views:99 Category:Python
-
DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns
Pandas Copy DataFrame
Publish Date:2025/05/01 Views:53 Category:Python
-
This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:
Pandas DataFrame.ix[] Function
Publish Date:2025/05/01 Views:168 Category:Python
-
Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice
Pandas DataFrame.describe() Function
Publish Date:2025/05/01 Views:120 Category:Python
-
Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a