Aggregation functions help us find simple summaries of data, like totals, averages, or how spread out numbers are.
Aggregation functions (sum, mean, std) in Data Analysis Python
data.sum()
data.mean()
data.std()These functions are often used on columns of data in tables (like pandas DataFrames or Series).
They return a single number summarizing the data.
import pandas as pd numbers = pd.Series([10, 20, 30, 40]) print(numbers.sum())
import pandas as pd numbers = pd.Series([10, 20, 30, 40]) print(numbers.mean())
import pandas as pd numbers = pd.Series([10, 20, 30, 40]) print(numbers.std())
This program shows how to use sum, mean, and std to summarize daily sales data.
import pandas as pd # Create a small dataset of daily sales sales = pd.Series([100, 150, 200, 130, 170]) # Calculate total sales total_sales = sales.sum() # Calculate average sales average_sales = sales.mean() # Calculate sales standard deviation sales_std = sales.std() print(f"Total sales: {total_sales}") print(f"Average sales: {average_sales}") print(f"Sales standard deviation: {sales_std:.2f}")
Standard deviation (std) tells you how much the numbers differ from the average.
If your data has missing values, these functions usually ignore them automatically.
You can use these functions on columns in tables to summarize each column.
Aggregation functions give quick summaries like total, average, and spread.
They help understand data without looking at every number.
sum(), mean(), and std() are common and easy to use in data analysis.
Practice
mean() function calculate in a dataset?Solution
Step 1: Understand the purpose of
Themean()mean()function calculates the average by adding all values and dividing by the count.Step 2: Compare with other options
Sum is total, range is difference, mode is most frequent. Only mean is average.Final Answer:
The average value of the data -> Option DQuick Check:
mean = average [OK]
- Confusing mean with sum
- Thinking mean finds the most frequent value
- Mixing mean with range or mode
data?Solution
Step 1: Recall pandas syntax for std()
In pandas, to get std of a column, usedf.column.std()ordf['column'].std().Step 2: Check each option
df.data.std()uses correct dot notation.df.std(data)is wrong syntax.std(df['data'])is not a pandas function call.df['data'].standard_deviation()uses a non-existent method.Final Answer:
df.data.std()-> Option CQuick Check:
Use dot notation with std() [OK]
df.column.std() to get standard deviation [OK]- Calling std() as a standalone function
- Using wrong method names
- Passing column name as argument to std()
import pandas as pd
df = pd.DataFrame({'values': [10, 20, 30, 40, 50]})
result = df['values'].sum()What is the value of
result?Solution
Step 1: Identify the data values
The column 'values' contains [10, 20, 30, 40, 50].Step 2: Calculate the sum
Sum = 10 + 20 + 30 + 40 + 50 = 150.Final Answer:
150 -> Option AQuick Check:
Sum of values = 150 [OK]
- Calculating mean instead of sum
- Adding only first two numbers
- Confusing sum with count
import pandas as pd numbers = [5, 10, 15] mean_value = numbers.mean()
What is the error and how to fix it?
Solution
Step 1: Identify the error cause
Python lists do not have amean()method, so callingnumbers.mean()causes an AttributeError.Step 2: Fix by converting list to pandas Series
Convert list topd.Series(numbers)then call.mean()to get the average.Final Answer:
Error: 'list' object has no attribute 'mean'. Fix by converting list to pandas Series first. -> Option AQuick Check:
Lists lack mean(), use pd.Series [OK]
- Calling mean() directly on list
- Trying to import mean() separately
- Assuming mean() needs arguments
df with a column scores containing exam scores. You want to find the average score but only for scores above 50. Which code correctly calculates this?Solution
Step 1: Filter scores greater than 50
Usedf[df['scores'] > 50]to select rows where scores are above 50.Step 2: Calculate mean of filtered scores
Access the 'scores' column of filtered data and call.mean()to get average.Final Answer:
df[df['scores'] > 50]['scores'].mean()-> Option BQuick Check:
Filter then mean = correct approach [OK]
- Passing condition inside mean()
- Dividing sum by 50 instead of count
- Using std() instead of mean()
Ready to prove it?
Pass the test and this pattern moves unknown
