Column Average in Record List - Python Last Updated : 30 Jan, 2025 Summarize Comments Improve Suggest changes Share 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) OutputAverage 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.Table of ContentUsing reduce() from functoolsUsing NumPyUsing PandasUsing reduce() from functoolsThis 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) OutputAverage 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 NumPyThis 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) OutputAverage score: 84.33333333333333 Explanation:numpy.array() converts the extracted values into a NumPy array.np.mean() computes the mean of the column values.Using PandasThis 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) OutputAverage score: 84.33333333333333 Explanation:The list of records is converted into a Pandas DataFrame.The mean() function calculates the average of the specified column. Comment More infoAdvertise with us Next Article Python - Convert Records List to Segregated Dictionary M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - Remove Record if Nth Column is K Sometimes while working with a list of records, we can have a problem in which we need to perform the removal of records on the basis of the presence of certain elements at the Nth position of the record. Let us discuss certain ways in which this task can be performed. Method #1: Using loop This is 10 min read Python - Convert Records List to Segregated Dictionary Sometimes, while working with Python records, we can have a problem in which we need to convert each element of tuple records into a separate key in dictionary, with each index having a different value. This kind of problem can have applications in data domains. Let us discuss certain ways in which 7 min read Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e 3 min read Print a List in Horizontally in Python Printing a list horizontally means displaying the elements of a list in a single line instead of each element being on a new line. In this article, we will explore some methods to print a list horizontally in Python.Using * (Unpacking Operator)unpacking operator (*) allows us to print list elements 2 min read Load CSV data into List and Dictionary using Python Prerequisites: Working with csv files in Python CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or m 2 min read Python - Records with Value at K index Sometimes, while working with records, we might have a problem in which we need to find all the tuples of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Letâs discuss c 8 min read Like