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.
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.