0
0
Pandasdata~30 mins

apply() on columns in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using apply() on DataFrame Columns with pandas
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(products['PriceEUR']) to show the new prices.