Pandas DataFrame mean()
0 745
Introduction to pandas DataFrame mean()
Calculating averages is a fundamental part of data analysis, and pandas provides a straightforward way to compute this through the mean() method for DataFrames. This method helps you find the mean value of numeric data along rows or columns efficiently.
What is DataFrame.mean()?
The mean() function in pandas DataFrame calculates the arithmetic mean (average) of the values for each column or row. By default, it operates column-wise, but you can change its behavior to compute the mean across rows as well.
Syntax of DataFrame.mean()
The basic syntax is:
DataFrame.mean(axis=0, skipna=True, numeric_only=None, **kwargs)
axis=0: Calculate mean for each column. Useaxis=1for rows.skipna=True: IgnoresNaNvalues by default.numeric_only=None: If True, include only numeric data.
Calculating Mean Column-wise
By default, mean() calculates the mean for each column, ignoring any missing values:
import pandas as pd
data = {
'Math': [85, 90, 78, 92],
'Physics': [88, 95, None, 85],
'Chemistry': [90, 85, 88, 86]
}
df = pd.DataFrame(data)
column_means = df.mean()
print(column_means)
Calculating Mean Row-wise
To compute the mean across rows instead of columns, specify axis=1:
row_means = df.mean(axis=1)
print(row_means)
Handling Missing Values
The mean() method ignores NaN values by default, which helps provide accurate results even if some data points are missing. You can change this behavior with the skipna parameter if needed.
Using numeric_only Parameter
If your DataFrame contains non-numeric data, setting numeric_only=True ensures that the mean calculation only considers numeric columns, avoiding errors or unexpected results.
Example: Calculating Mean in a DataFrame
Here's a complete example demonstrating both column-wise and row-wise mean calculations, including handling missing data:
import pandas as pd
data = {
'English': [75, 88, 92, None],
'Math': [89, 94, 85, 91],
'Science': [90, None, 88, 84]
}
df = pd.DataFrame(data)
# Column-wise mean
print("Column-wise mean:")
print(df.mean())
# Row-wise mean
print("\nRow-wise mean:")
print(df.mean(axis=1))
Summary
The pandas DataFrame mean() function is a simple yet powerful tool to calculate average values either across columns or rows. It handles missing data gracefully and offers flexibility to focus on numeric data only, making it an essential function for data analysis tasks.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:


Comments
Waiting for your comments