0
0
Data Analysis Pythondata~30 mins

apply() function for custom logic in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
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.