📖 Scenario: You work in a small store that tracks daily sales of two products. Sometimes, sales data for a product is missing on some days. You want to add the sales of both products day by day, but only where data for both products exists.
🎯 Goal: Build a Python program using pandas Series to add sales data of two products with automatic alignment by date. You will create the sales data, set a threshold for minimum sales, add the sales with alignment, and print the result.
📋 What You'll Learn
Create two pandas Series named sales_product_a and sales_product_b with sales data indexed by dates.
Create a variable min_sales to hold the minimum sales threshold.
Add the two Series using the add() method with fill_value=0 to handle missing dates.
Print the resulting Series showing total sales per date.
💡 Why This Matters
🌍 Real World
Stores and businesses often have sales data for different products recorded on different days. Adding sales data with alignment helps combine information correctly even if some days are missing for some products.
💼 Career
Data analysts and scientists use pandas Series arithmetic and alignment to clean, combine, and analyze time series data in retail, finance, and many other fields.
Progress0 / 4 steps
1
Create sales data for two products
Import pandas as pd. Create a pandas Series called sales_product_a with these values: 10 on '2024-06-01', 15 on '2024-06-02', and 7 on '2024-06-04'. Create another Series called sales_product_b with these values: 5 on '2024-06-01', 8 on '2024-06-03', and 12 on '2024-06-04'. Use the dates as the index for both Series.
Data Analysis Python
Hint
Use pd.Series with index=pd.to_datetime([...]) to create Series with date indexes.
2
Set minimum sales threshold
Create a variable called min_sales and set it to 10. This will be used later to filter total sales.
Data Analysis Python
Hint
Just create a variable named min_sales and assign the value 10.
3
Add sales data with alignment
Create a new Series called total_sales by adding sales_product_a and sales_product_b using the add() method. Use fill_value=0 to treat missing sales as zero.
Data Analysis Python
Hint
Use sales_product_a.add(sales_product_b, fill_value=0) to add with alignment.
4
Print total sales
Print the total_sales Series to see the combined sales per date.
Data Analysis Python
Hint
Use print(total_sales) to show the combined sales.
Practice
(1/5)
1. What happens when you add two pandas Series with different labels using + operator?
easy
A. The result is a Series with all zeros
B. Values are added ignoring labels, just by position
C. An error is raised due to label mismatch
D. Values are added only where labels match; others become NaN
Solution
Step 1: Understand pandas Series label alignment
When adding two Series, pandas aligns data by their labels (index) before performing arithmetic.
Step 2: Effect of label mismatch on addition
If a label is missing in one Series, the result for that label is NaN because pandas cannot add missing data.
Final Answer:
Values are added only where labels match; others become NaN -> Option D
Quick Check:
Series addition aligns labels = Values are added only where labels match; others become NaN [OK]
Hint: Remember: pandas adds by matching labels, not positions [OK]
Common Mistakes:
Assuming addition ignores labels and adds by position
Expecting an error on label mismatch
Thinking missing labels fill with zero automatically
2. Which of the following is the correct syntax to add two pandas Series s1 and s2 with label alignment, allowing specification of fill_value for missing labels?
easy
A. s1 + s2
B. s1 + s2.values
C. s1.add(s2)
D. s1.append(s2)
Solution
Step 1: Check syntax for adding Series with alignment and fill_value
Using s1.add(s2) adds Series with label alignment and allows fill_value for missing labels.
Step 2: Compare other options
s1 + s2 adds with alignment but no fill_value option. s1 + s2.values ignores labels (positional). s1.append(s2) concatenates.
Final Answer:
s1.add(s2) -> Option C
Quick Check:
Use .add() for aligned addition with fill_value [OK]
Hint: Use .add() method for safe label-aligned addition [OK]