📖 Scenario: You work in a small store that tracks daily sales. You have a list of sales amounts for each day. You want to calculate the total sales and also add tax to each sale. You can do this by using loops or by using faster vectorized operations with pandas.
🎯 Goal: Learn how to use vectorized operations in pandas to perform calculations on data quickly, instead of using slower loops.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data
Create a tax rate variable
Calculate total sales using a loop
Calculate sales with tax using vectorized operations
Print the results
💡 Why This Matters
🌍 Real World
Stores and businesses often need to calculate totals and apply taxes or discounts to many sales quickly.
💼 Career
Data scientists and analysts use vectorized operations in pandas to handle large datasets efficiently without slow loops.
Progress0 / 4 steps
1
Create the sales data
Create a pandas DataFrame called sales_data with one column named 'sales' and these exact values: 100, 150, 200, 130, 170.
Pandas
Hint
Use pd.DataFrame with a dictionary where the key is 'sales' and the value is the list of numbers.
2
Set the tax rate
Create a variable called tax_rate and set it to 0.1 (which means 10%).
Pandas
Hint
Just assign 0.1 to the variable tax_rate.
3
Calculate total sales using a loop
Create a variable called total_sales and set it to 0. Then use a for loop with the variable sale to go through sales_data['sales'] and add each sale to total_sales.
Pandas
Hint
Start with total_sales = 0. Then add each sale inside the loop using total_sales += sale.
4
Calculate sales with tax using vectorized operations and print results
Create a new column in sales_data called 'sales_with_tax' by multiplying sales_data['sales'] by (1 + tax_rate). Then print total_sales and print the sales_data DataFrame.
Pandas
Hint
Multiply the whole sales column by 1 + tax_rate to get the new column. Use print() to show results.
Practice
(1/5)
1. Which of the following best describes vectorized operations in pandas?
easy
A. They require writing explicit for-loops to manipulate data.
B. They process data one row at a time using loops.
C. They operate on entire columns or arrays at once, making them faster.
D. They are slower than loops for large datasets.
Solution
Step 1: Understand vectorized operations
Vectorized operations apply a function to whole columns or arrays simultaneously.
Step 2: Compare with loops
Loops process data row by row, which is slower for large data.
Final Answer:
They operate on entire columns or arrays at once, making them faster. -> Option C
Quick Check:
Vectorized = whole columns at once [OK]
Hint: Vectorized means whole column at once, not row by row [OK]
2. Which of the following is the correct syntax to add 10 to every element in a pandas Series s using vectorized operations?
easy
A. for i in s: s[i] = s[i] + 10
B. s = s + 10
C. s.apply(lambda x: x + 10)
D. s.add(10, inplace=True)
Solution
Step 1: Identify vectorized addition syntax
Adding a scalar to a Series directly uses vectorized addition: s = s + 10.
Step 2: Check other options
for i in s: s[i] = s[i] + 10 uses a loop incorrectly, C returns a new Series but does not assign, B is invalid because add does not have inplace.
Final Answer:
s = s + 10 -> Option B
Quick Check:
Use s + 10 for vectorized add [OK]
Hint: Use s + 10 for vectorized addition, no loops [OK]
Common Mistakes:
Trying to modify Series inside a for-loop
Using apply without assignment
Using invalid inplace parameter with add()
3. What will be the output of this code?
import pandas as pd
s = pd.Series([1, 2, 3])
result = []
for val in s:
result.append(val * 2)
print(result)
medium
A. Error: cannot multiply Series elements in loop
B. pd.Series([2, 4, 6])
C. [1, 2, 3]
D. [2, 4, 6]
Solution
Step 1: Understand the loop operation
The loop goes through each value in the Series and multiplies it by 2, appending to a list.
Step 2: Check the printed output
The printed output is the list [2, 4, 6], not a Series.
Final Answer:
[2, 4, 6] -> Option D
Quick Check:
Loop multiplies each value, list output [OK]
Hint: Loop appends multiplied values to list, not Series [OK]
Common Mistakes:
Expecting a Series output instead of list
Confusing loop with vectorized operation
Thinking loop causes error multiplying values
4. Identify the error in this code that tries to double values in a pandas Series s:
result = []
for i in range(len(s)):
result.append(s[i] * 2)
print(result)
medium
A. No error; code works correctly
B. Accessing Series elements by integer index is invalid
C. Using range(len(s)) causes an IndexError
D. Appending to list inside loop is not allowed
Solution
Step 1: Check indexing method
Accessing Series elements by integer position with s[i] works if Series index is default 0-based.
Step 2: Verify loop and append
The loop appends doubled values to list correctly; no error occurs.
Final Answer:
No error; code works correctly -> Option A
Quick Check:
Indexing by position works if default index [OK]
Hint: Default Series index allows integer access [OK]
Common Mistakes:
Assuming s[i] always causes error
Thinking append inside loop is invalid
Confusing label-based and position-based indexing
5. You have a DataFrame df with a column 'price'. You want to increase all prices by 5% efficiently. Which method is best?
hard
A. Use df['price'] = df['price'] * 1.05 to apply vectorized multiplication.
B. Use a for-loop to multiply each price by 1.05 and update the DataFrame row by row.
C. Use df.apply(lambda row: row['price'] * 1.05, axis=1) without assignment.
D. Convert the column to a list, multiply each element by 1.05 in a loop, then assign back.
Solution
Step 1: Identify efficient vectorized method
Multiplying the entire column by 1.05 directly uses vectorized operations and is fastest.
Step 2: Evaluate other options
Use a for-loop to multiply each price by 1.05 and update the DataFrame row by row. uses slow loops, C does not assign result back, D is inefficient with list conversion.
Final Answer:
Use df['price'] = df['price'] * 1.05 to apply vectorized multiplication. -> Option A
Quick Check:
Vectorized column operation is fastest [OK]
Hint: Multiply whole column directly for speed [OK]
Common Mistakes:
Using loops instead of vectorized operations
Forgetting to assign result back to DataFrame
Converting to list unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.