📖 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
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
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
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
Hint
Use print(products) to display the DataFrame.
Practice
(1/5)
1. What is the correct way to add a new column named age + 10 to a DataFrame df that adds 10 to the existing age column?
easy
A. df['age + 10'] == df['age'] + 10
B. df['age + 10'] = df['age'] + 10
C. df.add_column('age + 10', df['age'] + 10)
D. df.age + 10 = df.age + 10
Solution
Step 1: Understand how to create a new column in pandas
In pandas, new columns are created by assigning a value to df['new_column_name'].
Step 2: Apply the addition operation to the existing column
Adding 10 to the age column is done by df['age'] + 10. Assigning this to df['age + 10'] creates the new column.
Hint: Use df['new_col'] = expression to add columns [OK]
Common Mistakes:
Using double equals (==) instead of single equals (=) for assignment
Trying to use a method like add_column which doesn't exist
Using dot notation for new columns with spaces or special characters
2. Which of the following is the correct syntax to create a new column full_name by combining first_name and last_name columns with a space in between in DataFrame df?
easy
A. df['full_name'] = df['first_name'] + ' ' + df['last_name']
B. df['full_name'] = df['first_name'].append(' ').append(df['last_name'])
C. df['full_name'] = df['first_name'].join(' ', df['last_name'])
D. df['full_name'] = df['first_name'] & ' ' & df['last_name']
Solution
Step 1: Combine string columns with + operator
In pandas, string columns can be combined using the + operator. To add a space, include ' ' between them.
Step 2: Assign the combined string to a new column
Assign the result to df['full_name'] to create the new column.
Multiply each row's price by quantity: 100*2=200, 150*3=450, 200*1=200.
Step 2: Assign result to new column and output it
The new column 'total' will have values [200, 450, 200]. Printing df['total'] shows this list.
Final Answer:
[200, 450, 200] -> Option A
Quick Check:
Element-wise multiply = [200, 450, 200] [OK]
Hint: Multiply columns element-wise for new column [OK]
Common Mistakes:
Adding instead of multiplying columns
Confusing row-wise with column-wise operations
Expecting a scalar instead of a Series output
4. What is wrong with this code that tries to create a new column discounted_price by subtracting 10 from price in DataFrame df?
df.discounted_price = df['price'] - 10
medium
A. It will fail if the column name has spaces or special characters.
B. It will cause a syntax error because of dot notation.
C. It will create a new variable, not a DataFrame column.
D. It will create the column correctly.
Solution
Step 1: Understand dot notation for DataFrame columns
Dot notation df.col = value works to create new columns only if 'col' is a valid Python identifier (no spaces, no special characters).
Step 2: Check the column name 'discounted_price'
'discounted_price' consists of letters and underscores, which is valid. Therefore, the code creates the column correctly.
Final Answer:
It will create the column correctly. -> Option D
Quick Check:
Dot notation works for valid identifiers [OK]
Hint: Use df['col'] if column name has spaces or special chars [OK]
Common Mistakes:
Thinking dot notation fails for valid column names
Confusing DataFrame column assignment with local variable creation
Believing underscores invalidate column names for dot notation
5. You have a DataFrame df with columns score and max_score. You want to create a new column percentage that shows the score as a percentage of max_score, but if max_score is zero, set percentage to None. Which code correctly does this?
hard
A. df['percentage'] = df['score'] / df['max_score'].replace(0, None) * 100