0
0
Data Analysis Pythondata~5 mins

Aggregation functions (sum, mean, count) in Data Analysis Python

Choose your learning style9 modes available
Introduction

Aggregation functions help us quickly find totals, averages, or counts from data. They make it easy to understand big data by summarizing it.

You want to find the total sales from a list of daily sales numbers.
You need to calculate the average temperature from a week of weather data.
You want to count how many customers visited a store in a month.
You want to summarize survey results by counting responses.
You want to find the total number of products sold in different categories.
Syntax
Data Analysis Python
data['column_name'].sum()
data['column_name'].mean()
data['column_name'].count()

Replace data with your data table or DataFrame.

Replace column_name with the name of the column you want to analyze.

Examples
Calculates the total sales amount from the 'amount' column.
Data Analysis Python
sales['amount'].sum()
Finds the average daily temperature from the 'day_temp' column.
Data Analysis Python
temperature['day_temp'].mean()
Counts how many customer IDs are recorded, showing total visits.
Data Analysis Python
visits['customer_id'].count()
Sample Program

This program creates a small table of sales amounts for five days. It then calculates the total sales, average sales, and counts how many sales entries there are. Finally, it prints these results.

Data Analysis Python
import pandas as pd

# Create a simple data table with sales data
sales = pd.DataFrame({
    'day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
    'amount': [100, 150, 200, 130, 170]
})

# Calculate total sales
total_sales = sales['amount'].sum()

# Calculate average sales
average_sales = sales['amount'].mean()

# Count number of sales entries
count_sales = sales['amount'].count()

print(f"Total sales: {total_sales}")
print(f"Average sales: {average_sales}")
print(f"Number of sales entries: {count_sales}")
OutputSuccess
Important Notes

Aggregation functions ignore missing values (NaN) by default.

Use count() to count non-empty values, not the total rows.

Summary

Aggregation functions help summarize data quickly.

sum() adds values, mean() finds average, count() counts entries.

These functions work on columns of data in tables or DataFrames.