Pandas DataFrame DataFrame.mean() function
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis.
pandas.DataFrame.mean()
grammar
DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
parameter
axis |
Find the average along the 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 , count along a specific level |
numeric_only |
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 mean over the requested axis Series
, otherwise returns the average DataFrame
.
Example Code: DataFrame.mean()
Method to find the mean along the column axis
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 2, 3],
'Y': [4, 3, 8, 4]})
print("DataFrame:")
print(df)
means=df.mean()
print("Means of Each Column:")
print(means)
Output:
DataFrame:
X Y
0 1 4
1 2 3
2 2 8
3 3 4
Means of Each Column:
X 2.00
Y 4.75
dtype: float64
Computes the average of two columns, and , and returns a object containing the average for each X
column .Y
Series
In Pandas, if we want to find the mean of a column in a DataFrame, we just call mean()
the mean function on that column.
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 2, 3],
'Y': [4, 3, 8, 4]})
print("DataFrame:")
print(df)
means=df["X"].mean()
print("Mean of Column X:")
print(means)
Output:
DataFrame:
X Y
0 1 4
1 2 3
2 2 8
3 3 4
Mean of Column X:
2.0
It just gives the average of the values DataFrame
in X
the columns.
Example Code: DataFrame.mean()
Method to find the mean along the row axis
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 2, 3],
'Y': [4, 3, 8, 4]})
print("DataFrame:")
print(df)
means=df.mean(axis=1)
print("Mean of Rows:")
print(means)
Output:
DataFrame:
X Y
0 1 4
1 2 3
2 2 8
3 3 4
Mean of Rows:
0 2.5
1 2.5
2 5.0
3 3.5
dtype: float64
It calculates the average of all rows and returns a Series
object containing the average for each row.
In Pandas, if we want to find DataFrame
the mean of a row in , we just call mean()
the function to calculate the mean of that row.
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 2, 3],
'Y': [4, 3, 8, 4]})
print("DataFrame:")
print(df)
mean=df.iloc[[0]].mean(axis=1)
print("Mean of 1st Row:")
print(mean)
Output:
DataFrame:
X Y
0 1 4
1 2 3
2 2 8
3 3 4
Mean of 1st Row:
0 2.5
dtype: float64
It only gives DataFrame
the average of the first row of values in .
We use iloc
the method to select a row based on its index.
Example code: DataFrame.mean()
Method ignores NaN
value to find average
We use skipna
the default value of the parameter, which is , skipna=True
to find DataFrame
the mean of along the specified axis, ignoring NaN
the value of .
import pandas as pd
df = pd.DataFrame({'X': [1, 2, None, 3],
'Y': [4, 3, None, 4]})
print("DataFrame:")
print(df)
means=df.mean(skipna=True)
print("Mean of Columns")
print(means)
Output:
DataFrame:
X Y
0 1.0 4.0
1 2.0 3.0
2 NaN NaN
3 3.0 4.0
Mean of Columns
X 2.000000
Y 3.666667
dtype: float64
If we set skipna=True
, it will ignore the in DataFrame NaN
. It allows us to calculate DataFrame
the mean of along the column axis, ignoring NaN
the values.
import pandas as pd
df = pd.DataFrame({'X': [1, 2, None, 3],
'Y': [4, 3, 3, 4]})
print("DataFrame:")
print(df)
means=df.mean(skipna=False)
print("Mean of Columns")
print(means)
Output:
DataFrame:
X Y
0 1.0 4
1 2.0 3
2 NaN 3
3 3.0 4
Mean of Columns
X NaN
Y 3.5
dtype: float64
Here, we get the value X
of the mean of column NaN
since X
there are values in 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
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
How to Apply a Function to a Column in a Pandas Dataframe
Publish Date:2025/04/30 Views:50 Category:Python
-
In Pandas, you can transform and manipulate columns and DataFrames using methods apply() such as transform() and . The desired transformation is passed to these methods as a function argument. Each method has its own subtle differences and
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:124 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50