Open In App

Column Average in Record List – Python

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a list of records where each record contains multiple fields, the task is to compute the average of a specific column. Each record is represented as a dictionary or a list, and the goal is to extract values from the chosen column and calculate their average. Let’s explore different methods to achieve this.

Using List Comprehension and sum()

This method extracts the required column values using list comprehension and then computes the average using sum() and len().

Python
# List of records
records = [
    {"id": 1, "score": 85, "age": 20},
    {"id": 2, "score": 90, "age": 22},
    {"id": 3, "score": 78, "age": 21}
]

# Compute column average
column = "score"
values = [record[column] for record in records]
average = sum(values) / len(values)

# Output the result
print("Average score:", average)

Output
Average score: 84.33333333333333

Explanation:

  • The column values are extracted using list comprehension.
  • The sum() function computes the total sum of values.
  • The len() function counts the number of records to compute the average.

Let’s explore some more ways to calculate average in record list.

Using reduce() from functools

This method uses reduce() to accumulate the sum and then divides by the length of records.

Python
from functools import reduce

# List of records
records = [
    {"id": 1, "score": 85, "age": 20},
    {"id": 2, "score": 90, "age": 22},
    {"id": 3, "score": 78, "age": 21}
]

# Compute column average
column = "score"
total = reduce(lambda acc, rec: acc + rec[column], records, 0)
average = total / len(records)

# Output the result
print("Average score:", average)

Output
Average score: 84.33333333333333

Explanation:

  • reduce() accumulates the sum of values in the given column.
  • The final sum is divided by the number of records to get the average.

Using NumPy

This method uses NumPy to convert the extracted column values into an array and compute the mean using built-in numerical operations.

Python
import numpy as np

# List of records
records = [
    {"id": 1, "score": 85, "age": 20},
    {"id": 2, "score": 90, "age": 22},
    {"id": 3, "score": 78, "age": 21}
]

# Compute column average
column = "score"
values = np.array([record[column] for record in records])
average = np.mean(values)

# Output the result
print("Average score:", average)

Output
Average score: 84.33333333333333

Explanation:

Using Pandas

This method utilizes pandas to store the data in a DataFrame and calculate the column average using built-in aggregation functions.

Python
import pandas as pd

# List of records
records = [
    {"id": 1, "score": 85, "age": 20},
    {"id": 2, "score": 90, "age": 22},
    {"id": 3, "score": 78, "age": 21}
]

# Convert to DataFrame
df = pd.DataFrame(records)

# Compute column average
average = df["score"].mean()

# Output the result
print("Average score:", average)

Output
Average score: 84.33333333333333

Explanation:

  • The list of records is converted into a Pandas DataFrame.
  • The mean() function calculates the average of the specified column.


Next Article
Practice Tags :

Similar Reads