0
0
Pandasdata~20 mins

Why custom functions matter in Pandas - See It in Action

Choose your learning style9 modes available
Why custom functions matter
📖 Scenario: You work in a small store that tracks daily sales. You want to analyze sales data to find out which days had high sales and which had low sales. To do this efficiently, you will use a custom function to label each day's sales as 'High' or 'Low'.
🎯 Goal: Build a small pandas program that uses a custom function to label sales as 'High' or 'Low' based on a sales threshold.
📋 What You'll Learn
Create a pandas DataFrame with exact sales data
Define a sales threshold variable
Write a custom function to label sales as 'High' or 'Low'
Apply the custom function to the DataFrame
Print the updated DataFrame
💡 Why This Matters
🌍 Real World
Stores and businesses often analyze sales data to understand performance. Custom functions help automate labeling and categorizing data quickly.
💼 Career
Data analysts and scientists use custom functions in pandas to clean, transform, and analyze data efficiently in real jobs.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with two columns: 'Day' and 'Sales'. Use these exact values: Days are 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'. Sales are 200, 150, 300, 100, 250 respectively.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary containing the two columns.

2
Set the sales threshold
Create a variable called threshold and set it to 200. This will be the cutoff to decide if sales are high or low.
Pandas
Need a hint?

Just assign the number 200 to the variable threshold.

3
Write the custom function to label sales
Define a function called label_sales that takes one argument sale. The function should return the string 'High' if sale is greater than or equal to threshold, otherwise return 'Low'. Then, create a new column in sales_data called 'Label' by applying the label_sales function to the 'Sales' column.
Pandas
Need a hint?

Use def to create the function. Use apply() to run it on the sales column.

4
Print the updated DataFrame
Write a print statement to display the sales_data DataFrame with the new 'Label' column.
Pandas
Need a hint?

Use print(sales_data) to show the full table.