Python - Calculate the mean of column values of a Pandas DataFrame

To calculate the mean of column values in a Pandas DataFrame, use the mean() method. This method computes the arithmetic average of numeric columns, making it essential for data analysis tasks.

Basic Syntax

The basic syntax for calculating column mean is ?

# For a single column
dataframe['column_name'].mean()

# For all numeric columns
dataframe.mean()

Creating Sample DataFrames

Let's create two DataFrames to demonstrate mean calculations ?

import pandas as pd

# Create DataFrame1 with car data
dataFrame1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
    "Units": [100, 150, 110, 80, 110, 90]
})

print("DataFrame1:")
print(dataFrame1)
DataFrame1:
       Car  Units
0      BMW    100
1    Lexus    150
2     Audi    110
3    Tesla     80
4  Bentley    110
5   Jaguar     90

Calculating Mean of a Single Column

To find the mean of a specific column, use the column name with mean() ?

import pandas as pd

dataFrame1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
    "Units": [100, 150, 110, 80, 110, 90]
})

# Calculate mean of Units column
units_mean = dataFrame1['Units'].mean()
print("Mean of Units column:", units_mean)
Mean of Units column: 106.66666666666667

Example with Multiple DataFrames

Here's a complete example showing mean calculations for different DataFrames ?

import pandas as pd

# Create DataFrame1
dataFrame1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
    "Units": [100, 150, 110, 80, 110, 90]
})

print("DataFrame1:")
print(dataFrame1)
print("Mean of Units column from DataFrame1:", dataFrame1['Units'].mean())

# Create DataFrame2
dataFrame2 = pd.DataFrame({
    "Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],
    "Price": [8000, 500, 3000, 1500, 3000, 4000]
})

print("\nDataFrame2:")
print(dataFrame2)
print("Mean of Price column from DataFrame2:", dataFrame2['Price'].mean())
DataFrame1:
       Car  Units
0      BMW    100
1    Lexus    150
2     Audi    110
3    Tesla     80
4  Bentley    110
5   Jaguar     90
Mean of Units column from DataFrame1: 106.66666666666667

DataFrame2:
    Product  Price
0        TV   8000
1  PenDrive    500
2  HeadPhone   3000
3  EarPhone   1500
4       HDD   3000
5       SSD   4000
Mean of Price column from DataFrame2: 3333.3333333333335

Calculating Mean for All Numeric Columns

You can also calculate the mean for all numeric columns at once ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Product": ['A', 'B', 'C', 'D'],
    "Price": [100, 200, 150, 250],
    "Quantity": [10, 20, 15, 25]
})

print("DataFrame:")
print(dataFrame)
print("\nMean of all numeric columns:")
print(dataFrame.mean())
DataFrame:
  Product  Price  Quantity
0       A    100        10
1       B    200        20
2       C    150        15
3       D    250        25

Mean of all numeric columns:
Price       175.0
Quantity     17.5
dtype: float64

Conclusion

The mean() method in Pandas is straightforward for calculating column averages. Use dataframe['column'].mean() for single columns or dataframe.mean() for all numeric columns. This method automatically handles numeric data types and ignores non-numeric columns.

Updated on: 2026-03-26T02:01:51+05:30

839 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements