Bird
Raised Fist0
Data Analysis Pythondata~10 mins

Aggregation functions (sum, mean, count) in Data Analysis Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the total sales using sum.

Data Analysis Python
total_sales = df['sales'].[1]()
Drag options to blanks, or click blank then click option'
Amean
Bsum
Ccount
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean() instead of sum() which calculates average.
Using count() which counts entries, not sums values.
2fill in blank
medium

Complete the code to find the average age using mean.

Data Analysis Python
average_age = df['age'].[1]()
Drag options to blanks, or click blank then click option'
Amin
Bsum
Ccount
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() which adds values instead of averaging.
Using count() which counts entries, not averages.
3fill in blank
hard

Fix the error in the code to count the number of entries in the 'product' column.

Data Analysis Python
product_count = df['product'].[1]()
Drag options to blanks, or click blank then click option'
Acount
Bsum
Cmean
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() which adds values instead of counting.
Using mean() which calculates average, not count.
4fill in blank
hard

Fill both blanks to create a dictionary with total and average sales.

Data Analysis Python
sales_summary = {'total': df['sales'].[1](), 'average': df['sales'].[2]()}
Drag options to blanks, or click blank then click option'
Asum
Bmean
Ccount
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up sum and mean functions.
Using count instead of sum or mean.
5fill in blank
hard

Fill all three blanks to create a dictionary with count, total, and average of 'quantity'.

Data Analysis Python
quantity_stats = {'count': df['quantity'].[1](), 'total': df['quantity'].[2](), 'average': df['quantity'].[3]()}
Drag options to blanks, or click blank then click option'
Amean
Bsum
Ccount
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean for count or sum.
Mixing order of functions.

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

  1. Step 1: Understand the purpose of sum()

    The sum() function adds all values in a column to get their total.
  2. Step 2: Compare with other functions

    mean() finds average, count() counts entries, so they do different things.
  3. Final Answer:

    Adds all the numbers together to get a total -> Option C
  4. 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

  1. Step 1: Identify correct pandas syntax for mean()

    In pandas, to get the mean of a column, use df['column'].mean().
  2. 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.
  3. Final Answer:

    df['sales'].mean() -> Option D
  4. 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

  1. 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.
  2. Step 2: Count the entries in 'score'

    There are 5 numbers: 10, 20, 30, 40, 50, so count() returns 5.
  3. Final Answer:

    5 -> Option A
  4. 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

  1. Step 1: Understand how sum() handles None values

    In pandas, sum() ignores None (missing) values and sums only the numbers.
  2. Step 2: Calculate sum ignoring None

    Values are 5, None, 15, 20. Sum = 5 + 15 + 20 = 40.
  3. Final Answer:

    40 -> Option A
  4. Quick Check:

    sum() skips None and adds numbers [OK]
Hint: sum() ignores None/null values automatically [OK]
Common Mistakes:
  • Expecting sum() to return None if any missing
  • Getting error due to None value
  • Counting None as zero explicitly
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

  1. Step 1: Filter rows where sales > 0

    Use df[df['sales'] > 0] to keep only positive sales days.
  2. Step 2: Calculate mean of filtered sales

    Apply ['sales'].mean() on filtered data to get average of positive sales only.
  3. Final Answer:

    df[df['sales'] > 0]['sales'].mean() -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard