pandas
  1. pandas-dataframemean

DataFrame.mean() - ( Pandas DataFrame Basics )

Heading h2

Syntax

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

Example

import pandas as pd

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

print(df.mean(axis=0))

Output

A    2.0
B    5.0
C    8.0
dtype: float64

Explanation

The DataFrame.mean() function returns the mean (average) of values for the requested axis. You can request the mean across all columns (axis=0) or across all rows (axis=1).

By default, DataFrame.mean() skips missing values (NaN), but this can be changed by setting skipna=False.

You can also specify which columns or rows to include in the mean calculation by setting the level parameter.

Use

The DataFrame.mean() function is useful for calculating summary statistics of a Pandas DataFrame. For example, you can use it to calculate the average value of a specific column or across all columns.

Important Points

  • The DataFrame.mean() function calculates the mean of values for the requested axis
  • By default, skipna=True and level=None
  • You can choose which axis to calculate the mean (axis=0 for columns, axis=1 for rows)
  • You can specify which columns or rows to include in the mean calculation by setting the level parameter

Summary

In conclusion, the DataFrame.mean() function is a useful method for calculating the mean of values in a Pandas DataFrame. It can be used to calculate the average value of a specific column or across all columns. By default, the function skips missing values, but this behavior can be changed by setting skipna=False. Overall, DataFrame.mean() is a useful tool for calculating summary statistics of a DataFrame.

Published on: