Understanding DataFrame.mean() method in Pandas

Updated: February 20, 2024 By: Guest Contributor Post a comment

Introduction

Data manipulation and analysis form the backbone of data science, with pandas being one of the most powerful and widely used libraries in Python for these tasks. Among its functionalities, the DataFrame.mean() method is pivotal for statistical analyses, allowing users to compute the mean of the data across various axes. This tutorial aims to guide you through the nuances of the DataFrame.mean() method, providing a comprehensive understanding through a series of code examples.

First, ensure you have pandas installed:

pip install pandas

Syntax of DataFrame.mean()

The DataFrame.mean() method computes the mean of the values for the requested axis. If no axis is specified, it defaults to computing the column-wise mean. Use the following syntax:

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

Parameters:

  • axis: {0 or ‘index’, 1 or ‘column’} – Specify the axis for the mean calculation.
  • skipna: Boolean, default True – Whether to exclude NA/null values.
  • level: If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.
  • numeric_only: Boolean, default True – Include only float, int, boolean data.

Basic Usage

Starting with the basics, let’s create a simple DataFrame:

import pandas as pd

# Sample DataFrame
data = { 'A': [1, 2, 3], 'B': [4, 5, None], 'C': [7, 8, 9] }
df = pd.DataFrame(data)
print(df)

This gives us:

   A    B  C
0  1  4.0  7
1  2  5.0  8
2  3  NaN  9

Computing the column-wise mean:

mean_values = df.mean()
print(mean_values)

Output:

A    2.0
B    4.5
C    8.0
dtype: float64

Advanced Usage

Moving to more complex examples, let’s use some real-world data and demonstrate the use of other parameters.

Let’s assume you’ve loaded a dataset containing multiple columns, including some non-numeric ones:

To compute the mean excluding non-numeric types:

mean_values = df.mean(numeric_only=True)
print(mean_values)

Let’s calculate row-wise mean, excluding NA values:

mean_rows = df.mean(axis=1, skipna=True)
print(mean_rows)

For datasets with hierarchical indices:

# Assuming df has a MultiIndex
mean_level = df.mean(level=0)
print(mean_level)

Working with Time Series Data

Pandas is also adept at handling time series data. If your dataset includes datetime indices, calculating the mean over specific time intervals becomes very simple.

Example:

# Generating sample time series data
df_ts = pd.date_range(start='1/1/2020', periods=100)
df_ts['value'] = np.random.random(size=(100,))

# Monthly mean
df_ts_monthly_mean = df_ts.resample('M').mean()
print(df_ts_monthly_mean)

Note: This example assumes you have NumPy installed for generating random numbers:

pip install numpy

Conclusion

Through this tutorial, we explored the DataFrame.mean() method in pandas, an essential tool for statistical analysis. Starting from basic examples and gradually moving to more complex scenarios, we discussed how to accurately compute means across different axes, data types, and structures. Armed with these insights, you’re now better equipped to handle a wide range of data analysis tasks.