📖 Scenario: You are working as a data analyst for a small bakery. You want to analyze daily sales data in a way that you can easily repeat the analysis every day with new data. This means your steps should be clear and easy to follow again and again.
🎯 Goal: Build a simple, reproducible analysis pattern in Python that loads sales data, sets a sales threshold, filters the data for days with high sales, and prints the filtered results.
📋 What You'll Learn
Create a dictionary with daily sales data
Set a sales threshold variable
Filter the sales data using a dictionary comprehension
Print the filtered sales data
💡 Why This Matters
🌍 Real World
Businesses often need to analyze sales or other data repeatedly. Using clear steps and variables makes it easy to update and rerun the analysis with new data.
💼 Career
Data analysts and scientists use reproducible patterns to save time and avoid mistakes when working with data regularly.
Progress0 / 4 steps
1
DATA SETUP: Create the sales data dictionary
Create a dictionary called daily_sales with these exact entries: 'Monday': 150, 'Tuesday': 200, 'Wednesday': 50, 'Thursday': 300, 'Friday': 100.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary. Separate each day and sales number with a colon, and separate pairs with commas.
2
CONFIGURATION: Set the sales threshold
Create a variable called sales_threshold and set it to 150.
Data Analysis Python
Hint
Just assign the number 150 to the variable sales_threshold.
3
CORE LOGIC: Filter days with sales above the threshold
Create a new dictionary called high_sales_days using a dictionary comprehension. Include only the days from daily_sales where the sales value is greater than sales_threshold.
Data Analysis Python
Hint
Use a dictionary comprehension with for day, sales in daily_sales.items() and an if condition to filter.
4
OUTPUT: Print the filtered high sales days
Write a print statement to display the high_sales_days dictionary.
Data Analysis Python
Hint
Use print(high_sales_days) to show the filtered dictionary.
Practice
(1/5)
1. What is the main goal of reproducible analysis in data science?
easy
A. To ensure the analysis can be repeated with the same results
B. To make the code run faster
C. To use the latest software versions
D. To create complex visualizations
Solution
Step 1: Understand reproducible analysis meaning
Reproducible analysis means you can run your work again and get the same results.
Step 2: Identify the main goal
The main goal is to make sure the analysis is repeatable and reliable.
Final Answer:
To ensure the analysis can be repeated with the same results -> Option A
Quick Check:
Reproducible analysis = repeatable results [OK]
Hint: Focus on repeatability and consistent results [OK]
Common Mistakes:
Thinking speed or software version is the main goal
Confusing reproducibility with visualization quality
2. Which of the following is the correct way to define a function in Python for reproducible analysis?
easy
A. def load_data():
return pd.read_csv('data.csv')
B. function load_data() {
return read.csv('data.csv')
}
C. load_data() => pd.read_csv('data.csv')
D. def load_data:
return pd.read_csv('data.csv')
Solution
Step 1: Identify Python function syntax
Python functions start with 'def', followed by name and parentheses, then a colon.
Step 2: Check each option
def load_data():
return pd.read_csv('data.csv') uses correct Python syntax. function load_data() {
return read.csv('data.csv')
} is JavaScript style. load_data() => pd.read_csv('data.csv') is incorrect Python syntax. def load_data:
return pd.read_csv('data.csv') misses parentheses after function name.
Final Answer:
def load_data():\n return pd.read_csv('data.csv') -> Option A
Quick Check:
Python function = def name(): [OK]
Hint: Remember Python functions use def name(): [OK]
Common Mistakes:
Using JavaScript or other language syntax
Omitting parentheses after function name
Missing colon at the end of function definition
3. Given the code below, what will be the output of print(cleaned_data.shape)?
Original data has 3 rows and 2 columns. Rows with null are dropped by dropna(). Only the first row has no null values.
Step 2: Count remaining rows and columns
After dropna(), only 1 row remains with 2 columns, so shape is (1, 2).
Final Answer:
(1, 2) -> Option D
Quick Check:
dropna() removes rows with null = (1, 2) [OK]
Hint: dropna() removes rows with missing values [OK]
Common Mistakes:
Counting rows with null as valid
Confusing columns count
Assuming dropna() removes columns
4. Identify the error in the following reproducible analysis script:
def analyze_data():
data = pd.read_csv('data.csv')
cleaned = data.dropna
return cleaned.describe()
result = analyze_data()
print(result)
medium
A. Incorrect file path in read_csv
B. Missing parentheses after dropna method call
C. Missing return statement in function
D. print statement syntax error
Solution
Step 1: Check method usage in code
dropna is a method and must be called with parentheses: dropna()
Step 2: Identify error effect
Without parentheses, cleaned is a method object, not a DataFrame, causing error on describe() call.
Final Answer:
Missing parentheses after dropna method call -> Option B
Quick Check:
Method call needs () = error if missing [OK]
Hint: Always add () when calling methods [OK]
Common Mistakes:
Forgetting parentheses on method calls
Assuming dropna is a property, not a method
Ignoring error messages about method objects
5. You want to create a reproducible analysis script that loads data, cleans it by removing rows with missing values, and then calculates the mean of column 'score'. Which of the following code snippets correctly implements this pattern?
def load_data():
return pd.read_csv('data.csv')
def clean_data(df):
return df.dropna()
def analyze(df):
return df['score'].mean()
# Run full analysis
raw = load_data()
clean = clean_data(raw)
result = analyze(clean)
print(result) defines functions for loading, cleaning, and analyzing, then runs them in order, ensuring reproducibility.
Step 2: Verify correctness of each step
Functions use correct syntax, dropna() is called properly, and mean is calculated on cleaned data.
Final Answer:
The code that defines functions for loading, cleaning, and analyzing, then calls them sequentially -> Option C