Using apply() Function for Custom Logic in Data Science
📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. You have a table of sales data and want to add a new column that classifies each day's sales as 'High' or 'Low' based on a sales threshold.
🎯 Goal: Build a program that uses the apply() function to add a new column to a sales DataFrame. This new column will label each row as 'High' or 'Low' sales depending on a threshold you set.
📋 What You'll Learn
Create a pandas DataFrame with specific sales data
Define a sales threshold variable
Write a custom function to classify sales
Use the apply() function to apply this logic to the DataFrame
Print the updated DataFrame with the new classification column
💡 Why This Matters
🌍 Real World
Classifying data based on conditions is common in business to quickly understand performance, like sales levels or customer ratings.
💼 Career
Data analysts and scientists often use apply() to run custom logic on data columns for feature engineering and data cleaning.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with these exact columns and values: 'Day': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] 'Bread_Sales': [120, 80, 150, 90, 200]
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary containing the exact keys and lists for days and sales.
2
Set the sales threshold
Create a variable called sales_threshold and set it to 100. This will be the cutoff to decide if sales are 'High' or 'Low'.
Data Analysis Python
Hint
Just create a variable named sales_threshold and assign it the value 100.
3
Write a custom function and apply it
Define a function called classify_sales that takes one argument sales. It should return the string 'High' if sales is greater than or equal to sales_threshold, otherwise return 'Low'. Then, use apply() on the Bread_Sales column of sales_data with classify_sales to create a new column called Sales_Level.
Data Analysis Python
Hint
Write a function with an if-else to check sales against sales_threshold. Then use apply() on the Bread_Sales column to create Sales_Level.
4
Print the updated DataFrame
Write a print statement to display the sales_data DataFrame with the new Sales_Level column.
Data Analysis Python
Hint
Use print(sales_data) to show the full DataFrame with the new column.
Practice
(1/5)
1. What does the apply() function do in a pandas DataFrame?
easy
A. It merges two DataFrames together.
B. It sorts the DataFrame by a column.
C. It filters rows based on a condition.
D. It applies a custom function to each row or column.
Solution
Step 1: Understand the purpose of apply()
The apply() function is used to run a custom function on each row or column of a DataFrame.
Step 2: Compare with other DataFrame methods
Sorting, filtering, and merging are different operations and not done by apply().
Final Answer:
It applies a custom function to each row or column. -> Option D
Quick Check:
apply() = custom function application [OK]
Hint: Remember: apply() runs your function on rows or columns [OK]
Common Mistakes:
Confusing apply() with sort or filter methods
Thinking apply() merges DataFrames
Assuming apply() only works on columns
2. Which of the following is the correct syntax to apply a function f row-wise on a DataFrame df?
easy
A. df.apply(f, axis=0)
B. df.apply(f)
C. df.apply(f, axis=1)
D. df.apply(f, axis=2)
Solution
Step 1: Recall axis parameter meaning
In pandas, axis=1 means apply function row-wise, while axis=0 means column-wise.
Step 2: Identify correct axis for row-wise
Since the question asks for row-wise application, axis=1 is correct.
Final Answer:
df.apply(f, axis=1) -> Option C
Quick Check:
Row-wise apply = axis=1 [OK]
Hint: Use axis=1 for rows, axis=0 for columns [OK]
A. Using axis=0 applies function column-wise, causing a KeyError.
B. Lambda function syntax is incorrect.
C. DataFrame is missing required columns.
D. No error; code runs correctly.
Solution
Step 1: Check axis parameter usage
axis=0 applies function to each column (Series), not rows.
Step 2: Understand lambda function context
Lambda expects row with keys 'X' and 'Y', but column Series only has index labels, causing KeyError.
Final Answer:
Using axis=0 applies function column-wise, causing a KeyError. -> Option A
Quick Check:
axis=0 = columns, lambda expects rows [OK]
Hint: Use axis=1 for row-wise logic to avoid key errors [OK]
Common Mistakes:
Confusing axis=0 and axis=1
Expecting row keys when applying column-wise
Ignoring error messages about missing keys
5. You have a DataFrame df with columns "score" and "passed" (boolean). You want to create a new column "grade" where: - If passed is True and score >= 90, grade is 'A' - If passed is True and score >= 75 but < 90, grade is 'B' - Otherwise, grade is 'F' Which apply() function correctly implements this logic?
hard
A. df['grade'] = df.apply(lambda r: 'A' if r['passed'] and r['score'] >= 90 else ('B' if r['passed'] and r['score'] >= 75 else 'F'), axis=1)
B. df['grade'] = df.apply(lambda r: 'A' if r['score'] >= 90 else 'B' if r['score'] >= 75 else 'F', axis=0)
C. df['grade'] = df.apply(lambda r: 'A' if r['passed'] or r['score'] >= 90 else 'F', axis=1)
D. df['grade'] = df.apply(lambda r: 'A' if r['passed'] and r['score'] > 90 else 'B', axis=1)
df['grade'] = df.apply(lambda r: 'A' if r['passed'] and r['score'] >= 90 else ('B' if r['passed'] and r['score'] >= 75 else 'F'), axis=1) correctly checks if passed is True and score thresholds for 'A' and 'B', else 'F'.
Step 3: Identify errors in other options
df['grade'] = df.apply(lambda r: 'A' if r['score'] >= 90 else 'B' if r['score'] >= 75 else 'F', axis=0) uses axis=0 (wrong axis). df['grade'] = df.apply(lambda r: 'A' if r['passed'] or r['score'] >= 90 else 'F', axis=1) uses 'or' instead of 'and'. df['grade'] = df.apply(lambda r: 'A' if r['passed'] and r['score'] > 90 else 'B', axis=1) misses 'F' grade and uses wrong comparison for 90.
Final Answer:
df['grade'] = df.apply(lambda r: 'A' if r['passed'] and r['score'] >= 90 else ('B' if r['passed'] and r['score'] >= 75 else 'F'), axis=1) -> Option A
Quick Check:
Correct logic and axis=1 for row-wise apply [OK]
Hint: Use axis=1 and nested if-else for multi-condition grading [OK]
Common Mistakes:
Using axis=0 instead of axis=1
Using 'or' instead of 'and' in conditions
Missing else condition for failing grade
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.