📖 Scenario: You work in a small store and keep track of product prices and quantities sold in a table. You want to calculate the total sales amount for each product.
🎯 Goal: Create a pandas DataFrame with product data, set a tax rate, calculate total sales including tax for each product, and display the results.
📋 What You'll Learn
Create a pandas DataFrame with product names, prices, and quantities
Create a variable for the tax rate
Calculate total sales including tax for each product using arithmetic operations on columns
Print the resulting DataFrame with the new total sales column
💡 Why This Matters
🌍 Real World
Stores and businesses often calculate total sales including taxes to understand revenue.
💼 Career
Data analysts and data scientists use pandas to manipulate sales data and perform calculations for reports.
Progress0 / 4 steps
1
Create the product DataFrame
Import pandas as pd and create a DataFrame called products with these exact columns and values: 'Product' with ['Pen', 'Notebook', 'Eraser'], 'Price' with [1.5, 3.0, 0.5], and 'Quantity' with [10, 5, 20].
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column values.
2
Set the tax rate
Create a variable called tax_rate and set it to 0.1 to represent 10% tax.
Pandas
Hint
Just assign 0.1 to the variable tax_rate.
3
Calculate total sales including tax
Create a new column in products called Total_Sales that calculates the total sales amount including tax for each product. Use the formula: Price * Quantity * (1 + tax_rate).
Pandas
Hint
Use arithmetic operations on the DataFrame columns and assign the result to a new column.
4
Display the updated DataFrame
Print the products DataFrame to display the product details along with the new Total_Sales column.
Pandas
Hint
Use print(products) to show the DataFrame.
Practice
(1/5)
1. What does the expression df['total'] = df['price'] + df['tax'] do in pandas?
easy
A. Multiplies the 'price' and 'tax' columns and stores in 'total'.
B. Deletes the 'price' and 'tax' columns from the DataFrame.
C. Filters rows where 'price' plus 'tax' is greater than zero.
D. Creates a new column 'total' by adding 'price' and 'tax' columns element-wise.
Solution
Step 1: Understand the operation on columns
The expression adds values from 'price' and 'tax' columns for each row.
Step 2: Assign result to new column
The sum is stored in a new column named 'total' in the DataFrame.
Final Answer:
Creates a new column 'total' by adding 'price' and 'tax' columns element-wise. -> Option D
Quick Check:
Adding columns creates new column with sums [OK]
Hint: Adding columns creates a new column with their sums [OK]
Common Mistakes:
Thinking it multiplies instead of adds
Assuming it deletes columns
Confusing assignment with filtering
2. Which of the following is the correct syntax to subtract column 'B' from column 'A' in a pandas DataFrame df and store the result in a new column 'C'?
easy
A. df['C'] = df['A'] - df['B']
B. df['C'] = df['A'] + df['B']
C. df['C'] = df['A'] * df['B']
D. df['C'] = df['A'] / df['B']
Solution
Step 1: Identify the subtraction operation
We want to subtract column 'B' from 'A', so the operator is '-'.
Step 2: Assign the result to new column 'C'
Use df['C'] = df['A'] - df['B'] to store the difference.
Final Answer:
df['C'] = df['A'] - df['B'] -> Option A
Quick Check:
Subtraction uses '-' operator between columns [OK]
Hint: Use '-' operator between columns to subtract [OK]
The output shows the values [6, 20] for the two rows.
Final Answer:
[6, 20] -> Option A
Quick Check:
Element-wise multiplication = [6, 20] [OK]
Hint: Multiply columns element-wise to get new column values [OK]
Common Mistakes:
Adding instead of multiplying
Printing wrong column
Confusing list output format
4. What is wrong with this code snippet?
df['D'] = df['A'] + '5'
Assuming 'A' is a numeric column.
medium
A. It will create a new column 'D' with all values as '5'.
B. It will add 5 to each value in column 'A' correctly.
C. It will raise a TypeError because you cannot add a string to a numeric column.
D. It will concatenate the string '5' to each numeric value in 'A'.
Solution
Step 1: Check data types involved in addition
Column 'A' is numeric, but '5' is a string, so adding them causes a type mismatch.
Step 2: Understand pandas behavior on mixed types
Pandas cannot add string to numeric directly, so it raises a TypeError.
Final Answer:
It will raise a TypeError because you cannot add a string to a numeric column. -> Option C
Quick Check:
Adding string to number causes TypeError [OK]
Hint: Check data types before arithmetic to avoid errors [OK]
Common Mistakes:
Assuming string '5' is converted automatically
Expecting concatenation with numbers
Ignoring error messages
5. You have a DataFrame df with columns 'cost' and 'discount'. You want to create a new column 'final_price' that subtracts the discount from cost and then applies a 10% tax on the result. Which code correctly does this?
hard
A. df['final_price'] = df['cost'] * 1.10 - df['discount']
B. df['final_price'] = (df['cost'] - df['discount']) * 1.10
C. df['final_price'] = df['cost'] - (df['discount'] * 1.10)
D. df['final_price'] = df['cost'] - df['discount'] * 1.10
Solution
Step 1: Subtract discount from cost
Calculate the price after discount: df['cost'] - df['discount'].
Step 2: Apply 10% tax on discounted price
Multiply the result by 1.10 to add 10% tax.
Step 3: Assign to 'final_price'
Store the final value in the new column 'final_price'.
Final Answer:
df['final_price'] = (df['cost'] - df['discount']) * 1.10 -> Option B
Quick Check:
Subtract then multiply for tax = correct formula [OK]
Hint: Use parentheses to control order: subtract first, then multiply [OK]
Common Mistakes:
Missing parentheses causing wrong order
Multiplying discount instead of discounted price
Applying tax before subtracting discount
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.