📖 Scenario: You work in a small store and want to analyze daily sales data to understand how much money you made, the average sale amount, and how many sales you had.
🎯 Goal: Build a simple Python program that uses aggregation functions sum, mean, and count to analyze sales data stored in a list.
📋 What You'll Learn
Create a list of sales amounts
Create a variable to count the number of sales
Calculate the total sales using sum
Calculate the average sale amount using mean
Print the total sales, average sale, and count of sales
💡 Why This Matters
🌍 Real World
Stores and businesses often analyze sales data to understand performance and make decisions.
💼 Career
Data analysts use aggregation functions like sum, mean, and count to summarize data quickly and clearly.
Progress0 / 4 steps
1
Create the sales data list
Create a list called sales with these exact values: 100, 250, 75, 300, 150.
Data Analysis Python
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Count the number of sales
Create a variable called count_sales and set it to the number of items in the sales list using the len() function.
Data Analysis Python
Hint
Use len(sales) to get the number of sales.
3
Calculate total and average sales
Create a variable called total_sales and set it to the sum of the sales list using the sum() function. Then create a variable called average_sale and set it to total_sales divided by count_sales.
Data Analysis Python
Hint
Use sum(sales) to add all sales. Then divide by count_sales to get the average.
4
Print the results
Print the values of total_sales, average_sale, and count_sales each on a new line.
Data Analysis Python
Hint
Use three print() statements, one for each variable.
Practice
(1/5)
1. What does the sum() function do when applied to a column of numbers in a DataFrame?
easy
A. Counts how many numbers are in the column
B. Finds the middle value of the numbers
C. Adds all the numbers together to get a total
D. Sorts the numbers from smallest to largest
Solution
Step 1: Understand the purpose of sum()
The sum() function adds all values in a column to get their total.
Step 2: Compare with other functions
mean() finds average, count() counts entries, so they do different things.
Final Answer:
Adds all the numbers together to get a total -> Option C
Quick Check:
sum() = total of values [OK]
Hint: Sum adds values, mean averages, count counts entries [OK]
Common Mistakes:
Confusing sum() with mean()
Thinking sum() counts entries
Assuming sum() sorts data
2. Which of the following is the correct way to calculate the average of a DataFrame column named sales using pandas?
easy
A. df.sales.sum() / df.sales.count()
B. df.mean('sales')
C. mean(df.sales)
D. df['sales'].mean()
Solution
Step 1: Identify correct pandas syntax for mean()
In pandas, to get the mean of a column, use df['column'].mean().
Step 2: Check other options
df.mean('sales') is invalid syntax; mean(df.sales) is not pandas method; df.sales.sum() / df.sales.count() works but is longer and less direct.
Final Answer:
df['sales'].mean() -> Option D
Quick Check:
Use df['column'].mean() for average [OK]
Hint: Use df['col'].mean() to get average quickly [OK]
Common Mistakes:
Using mean() as a standalone function
Passing column name as argument to df.mean()
Dividing sum by count manually instead of mean()
3. Given the DataFrame df below:
import pandas as pd
data = {'score': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
What is the output of df['score'].count()?
medium
A. 5
B. 150
C. 30
D. Error
Solution
Step 1: Understand what count() does on a column
count() counts the number of non-missing entries in the column. Here, all 5 values are present.
Step 2: Count the entries in 'score'
There are 5 numbers: 10, 20, 30, 40, 50, so count() returns 5.
Final Answer:
5 -> Option A
Quick Check:
count() = number of entries [OK]
Hint: count() returns number of non-empty entries [OK]
Common Mistakes:
Confusing count() with sum()
Expecting count() to return total value
Thinking count() returns average
4. Consider this code snippet:
import pandas as pd
data = {'values': [5, None, 15, 20]}
df = pd.DataFrame(data)
result = df['values'].sum()
What is the value of result?
medium
A. 40
B. None
C. Error
D. 20
Solution
Step 1: Understand how sum() handles None values
In pandas, sum() ignores None (missing) values and sums only the numbers.
Step 2: Calculate sum ignoring None
Values are 5, None, 15, 20. Sum = 5 + 15 + 20 = 40.
5. You have a DataFrame df with a column sales containing daily sales numbers. You want to find the average sales but only for days where sales were above zero. Which code correctly calculates this?
hard
A. df['sales'].mean()
B. df[df['sales'] > 0]['sales'].mean()
C. df['sales'].sum() / df['sales'].count()
D. df['sales'].count() / df['sales'].sum()
Solution
Step 1: Filter rows where sales > 0
Use df[df['sales'] > 0] to keep only positive sales days.
Step 2: Calculate mean of filtered sales
Apply ['sales'].mean() on filtered data to get average of positive sales only.
Final Answer:
df[df['sales'] > 0]['sales'].mean() -> Option B
Quick Check:
Filter then mean() for conditional average [OK]
Hint: Filter data first, then apply mean() for conditional average [OK]
Common Mistakes:
Using mean() without filtering
Dividing sum by count without filtering
Swapping sum and count in division
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.