📖 Scenario: You work in a small store and have a table of products with their prices in dollars. You want to convert these prices to euros to see how much they cost in Europe.
🎯 Goal: You will create a pandas DataFrame with product names and prices in dollars. Then, you will use the apply() function on the price column to convert all prices to euros using a fixed exchange rate.
📋 What You'll Learn
Create a pandas DataFrame with product names and prices in dollars
Create a variable for the exchange rate from dollars to euros
Use apply() on the price column to convert prices to euros
Print the new column with prices in euros
💡 Why This Matters
🌍 Real World
Stores often need to convert prices between currencies to sell products internationally.
💼 Career
Data analysts use pandas apply() to quickly transform and clean data columns for reports and dashboards.
Progress0 / 4 steps
1
Create the products DataFrame
Create a pandas DataFrame called products with two columns: 'Product' and 'PriceUSD'. The products and prices should be exactly: 'Apple', 1.2 'Banana', 0.5 'Cherry', 2.5
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column values.
2
Set the exchange rate
Create a variable called usd_to_eur and set it to 0.85 to represent the exchange rate from US dollars to euros.
Pandas
Hint
Just assign the number 0.85 to the variable usd_to_eur.
3
Convert prices to euros using apply()
Use the apply() function on the PriceUSD column of products to create a new column called PriceEUR. The conversion should multiply each price by usd_to_eur.
Pandas
Hint
Use products['PriceUSD'].apply(lambda x: x * usd_to_eur) to multiply each price by the exchange rate.
4
Print the new prices in euros
Print the PriceEUR column of the products DataFrame.
Pandas
Hint
Use print(products['PriceEUR']) to show the new prices.
Practice
(1/5)
1. What does the apply() function do when used on a pandas DataFrame column?
easy
A. It applies a function to each value in the column.
B. It deletes the column from the DataFrame.
C. It sorts the column values in ascending order.
D. It merges the column with another DataFrame.
Solution
Step 1: Understand the purpose of apply() on columns
The apply() method runs a function on each element of the column, transforming or analyzing the data.
Step 2: Compare with other options
Deleting, sorting, or merging are not actions performed by apply() on a column.
Final Answer:
It applies a function to each value in the column. -> Option A
Quick Check:
apply() runs function on each column value [OK]
Hint: Remember: apply() runs a function on each column value [OK]
Common Mistakes:
Thinking apply() deletes or sorts data
Confusing apply() with merge or drop functions
Assuming apply() works on entire DataFrame without axis
2. Which of the following is the correct syntax to apply a lambda function that doubles each value in the 'age' column of a DataFrame df?
easy
A. df['age'].apply = lambda x: x * 2
B. df.apply['age'](lambda x: x * 2)
C. df['age'].apply(lambda x: x * 2)
D. apply(df['age'], lambda x: x * 2)
Solution
Step 1: Identify correct method call syntax
The correct way to use apply on a column is df['column'].apply(function). df['age'].apply(lambda x: x * 2) matches this.
Step 2: Check lambda function usage
df['age'].apply(lambda x: x * 2) correctly passes a lambda function that doubles the value. Other options misuse syntax or assignment.
Final Answer:
df['age'].apply(lambda x: x * 2) -> Option C
Quick Check:
Use df['col'].apply(lambda x: ...) [OK]
Hint: Use df['col'].apply(lambda x: ...) for column functions [OK]
Common Mistakes:
Using square brackets with apply like a list
Assigning apply instead of calling it
Calling apply as a standalone function
3. Given the DataFrame df below:
score
0 10
1 20
2 30
3 40
What is the output of df['score'].apply(lambda x: x // 15)?
medium
A. 0 10
1 20
2 30
3 40
Name: score, dtype: int64
B. 0 0
1 1
2 2
3 3
Name: score, dtype: int64
C. 0 0
1 0
2 1
3 2
Name: score, dtype: int64
D. 0 0
1 1
2 2
3 2
Name: score, dtype: int64
Solution
Step 1: Apply the lambda function to each value
Divide each score by 15 using floor division: 10 // 15 = 0 20 // 15 = 1 30 // 15 = 2 40 // 15 = 2
Hint: Apply lambda to each value and calculate carefully [OK]
Common Mistakes:
Using normal division instead of floor division
Misreading floor division results
Confusing output format with DataFrame instead of Series
4. What is wrong with this code snippet?
df['new_col'] = df['value'].apply(x: x + 5)
How can it be fixed?
medium
A. Missing lambda keyword; fix: df['value'].apply(lambda x: x + 5)
B. apply() cannot add numbers; use map() instead
C. Should use square brackets around x; fix: apply([x]: x + 5)
D. Function must be defined outside apply; fix: def f(x): return x+5
Solution
Step 1: Identify syntax error in lambda function
The code uses x: x + 5 without the lambda keyword, which is required to define an anonymous function.
Step 2: Correct the syntax
Add lambda before the function: df['value'].apply(lambda x: x + 5).
Final Answer:
Missing lambda keyword; fix: df['value'].apply(lambda x: x + 5) -> Option A
Quick Check:
Lambda keyword needed for inline functions [OK]
Hint: Always use 'lambda' before inline functions in apply() [OK]
Common Mistakes:
Omitting the lambda keyword
Using incorrect brackets or syntax for lambda
Thinking apply() can't use inline functions
5. You have a DataFrame df with a column 'temperature' in Celsius. You want to create a new column 'temp_f' with Fahrenheit values using apply(). Which code correctly does this?
hard
A. df['temp_f'] = df.apply(lambda c: (c * 9/5) + 32)
B. df['temp_f'] = df['temperature'].apply(lambda c: (c * 9/5) + 32)
C. df['temp_f'] = df['temperature'].apply(lambda c: c + 32 * 9/5)
D. df['temp_f'] = df['temperature'].apply(lambda c: c * (9/5 + 32))
Solution
Step 1: Recall the formula for Celsius to Fahrenheit
The correct formula is Fahrenheit = (Celsius * 9/5) + 32.
Step 2: Check each option's lambda function
df['temp_f'] = df['temperature'].apply(lambda c: (c * 9/5) + 32) correctly applies the formula to each value in 'temperature'. df['temp_f'] = df.apply(lambda c: (c * 9/5) + 32) applies to whole DataFrame, C and D have incorrect formulas.