0
0
Pandasdata~15 mins

Creating new columns in Pandas - Try It Yourself

Choose your learning style9 modes available
Creating new columns
📖 Scenario: You work in a small store that keeps track of products and their prices. You want to add new information to your data to help with sales.
🎯 Goal: Create a new column in a pandas DataFrame that shows the price after a 10% discount.
📋 What You'll Learn
Create a pandas DataFrame with product names and prices
Create a variable for the discount rate
Add a new column with discounted prices using the discount rate
Print the updated DataFrame
💡 Why This Matters
🌍 Real World
Stores often need to update prices with discounts or taxes to help customers and manage sales.
💼 Career
Data analysts and data scientists use pandas to prepare and analyze sales data, including creating new columns for calculations.
Progress0 / 4 steps
1
Create the initial DataFrame
Create a pandas DataFrame called products with two columns: 'Product' and 'Price'. The DataFrame should have these exact rows: 'Apple' with price 1.0, 'Banana' with price 0.5, and 'Cherry' with price 2.0.
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 discount rate
Create a variable called discount_rate and set it to 0.10 to represent a 10% discount.
Pandas
Need a hint?

Just assign 0.10 to the variable discount_rate.

3
Add a new column with discounted prices
Create a new column in the products DataFrame called 'Discounted Price'. Calculate it by subtracting the discount from the original 'Price' using the discount_rate variable.
Pandas
Need a hint?

Multiply the 'Price' column by (1 - discount_rate) to get the discounted price.

4
Print the updated DataFrame
Print the products DataFrame to show the new 'Discounted Price' column.
Pandas
Need a hint?

Use print(products) to display the DataFrame.