Descriptive statistics review in Data Analysis Python - Time & Space Complexity
We want to understand how the time to calculate descriptive statistics changes as the data size grows.
How does the work increase when we have more data points?
Analyze the time complexity of the following code snippet.
import pandas as pd
def describe_data(df):
mean_val = df['value'].mean()
median_val = df['value'].median()
std_val = df['value'].std()
count_val = df['value'].count()
return mean_val, median_val, std_val, count_val
This code calculates basic descriptive statistics (mean, median, standard deviation, count) on one column of a data table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning through the column values to compute each statistic.
- How many times: Each statistic requires going through the data once or a few times.
As the number of data points increases, the time to compute each statistic grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations per statistic |
| 100 | About 100 operations per statistic |
| 1000 | About 1000 operations per statistic |
Pattern observation: The work grows linearly with the number of data points.
Time Complexity: O(n)
This means the time to calculate descriptive statistics grows directly with the size of the data.
[X] Wrong: "Calculating mean and median takes the same time regardless of data size."
[OK] Correct: Both mean and median require looking at the data values, so more data means more work.
Understanding how descriptive statistics scale helps you explain data processing steps clearly and shows you can think about efficiency in real tasks.
"What if we added sorting to find the median? How would the time complexity change?"
Practice
median in a dataset?Solution
Step 1: Understand what median means
The median is the value that lies in the middle of a sorted list of numbers.Step 2: Compare with other options
Mode is the most frequent value, mean is the average, and range is max-min.Final Answer:
The middle value when data is sorted from smallest to largest -> Option AQuick Check:
Median = middle value [OK]
- Confusing median with mean
- Thinking median is the most frequent value
- Mixing median with range
df?Solution
Step 1: Recall pandas method for summary
In pandas,describe()is the method to get descriptive stats.Step 2: Check other options
Methods likesummary(),stats()do not exist in pandas, anddescribe(df)is not valid syntax.Final Answer:
df.describe() -> Option CQuick Check:
Use df.describe() for stats [OK]
- Using non-existent methods like summary()
- Calling describe as a function with df argument
- Confusing with other libraries
print(df.mean())?
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)Solution
Step 1: Calculate mean for each column
Column A values: 1, 2, 3; mean = (1+2+3)/3 = 2.0. Column B values: 4, 5, 6; mean = (4+5+6)/3 = 5.0.Step 2: Understand pandas output format
Printing df.mean() shows means per column with dtype float64.Final Answer:
A 2.0 B 5.0 dtype: float64 -> Option AQuick Check:
Mean = sum/number of values [OK]
- Calculating median instead of mean
- Mixing up column values
- Expecting integer dtype instead of float
df. What is the error?
import pandas as pd
data = {'X': [10, 20, 30, 40]}
df = pd.DataFrame(data)
std_dev = df.std('X')
print(std_dev)Solution
Step 1: Check std() method usage
In pandas, std() calculates standard deviation for all numeric columns or Series. It does not accept a column name as argument.Step 2: Correct way to get std of column 'X'
We must select the column first:df['X'].std().Final Answer:
The std() method does not take any arguments; column must be selected first -> Option BQuick Check:
Use df['X'].std() to get column std [OK]
- Passing column name as argument to std()
- Using variance() instead of std()
- Assuming std() needs axis=1
df with columns 'Age' and 'Salary'. You want to find the average salary for people older than 30. Which code correctly does this?Solution
Step 1: Filter rows where Age > 30
Use boolean indexing:df[df['Age'] > 30]selects rows with Age greater than 30.Step 2: Select 'Salary' column and calculate mean
From filtered rows, select 'Salary' column and callmean()to get average salary.Final Answer:
df[df['Age'] > 30]['Salary'].mean() -> Option DQuick Check:
Filter then mean for conditional average [OK]
- Passing condition inside mean()
- Trying to filter after mean()
- Using mean() on entire DataFrame
Ready to prove it?
Pass the test and this pattern moves unknown
