Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print the mean of a Pandas series
The mean() function in the Pandas library can be used to find the arithmetic mean (average) of a series. This function calculates the sum of all values divided by the number of values.
Syntax
Series.mean(axis=None, skipna=True, level=None, numeric_only=None)
Parameters
The key parameters are:
- skipna: If True (default), excludes NaN values from calculation
- numeric_only: Include only numeric columns
Example
Here's how to calculate the mean of a Pandas series ?
import pandas as pd
series = pd.Series([10, 20, 30, 40, 50])
print("Pandas Series:")
print(series)
series_mean = series.mean()
print("Mean of the Pandas series:", series_mean)
Pandas Series: 0 10 1 20 2 30 3 40 4 50 dtype: int64 Mean of the Pandas series: 30.0
Handling Missing Values
The mean() function automatically excludes NaN values by default ?
import pandas as pd
import numpy as np
series_with_nan = pd.Series([10, 20, np.nan, 40, 50])
print("Series with NaN:")
print(series_with_nan)
mean_value = series_with_nan.mean()
print("Mean (excluding NaN):", mean_value)
Series with NaN: 0 10.0 1 20.0 2 NaN 3 40.0 4 50.0 dtype: float64 Mean (excluding NaN): 30.0
Conclusion
The mean() function provides a simple way to calculate the average of a Pandas series. It automatically handles missing values by excluding them from the calculation, making it robust for real-world data analysis.
Advertisements
