📖 Scenario: You work in a small grocery store. You have a table of products with their prices and quantities. You want to add a new column for the total value of each product (price times quantity). Later, you decide to remove the quantity column to keep the table simple.
🎯 Goal: Create a data table using a dictionary, add a new column for total value, then remove the quantity column, and finally display the updated table.
📋 What You'll Learn
Create a dictionary called products with keys 'Name', 'Price', and 'Quantity' and the exact values specified.
Create a new column 'TotalValue' by multiplying Price and Quantity for each product.
Remove the 'Quantity' column from the dictionary.
Print the final products dictionary.
💡 Why This Matters
🌍 Real World
Stores and businesses often keep product data in tables. Adding and removing columns helps update the data as needed.
💼 Career
Data analysts and scientists frequently manipulate tables by adding calculated columns and cleaning data by removing unnecessary columns.
Progress0 / 4 steps
1
Create the initial products dictionary
Create a dictionary called products with these exact keys and values: 'Name' with the list ['Apple', 'Banana', 'Carrot'], 'Price' with the list [0.5, 0.3, 0.2], and 'Quantity' with the list [10, 20, 15].
Data Analysis Python
Hint
Use a dictionary with keys 'Name', 'Price', and 'Quantity'. Each key should have a list of values.
2
Add a new column for total value
Create a new key 'TotalValue' in the products dictionary. Set its value to a list where each element is the product of the corresponding Price and Quantity values.
Data Analysis Python
Hint
Use a list comprehension with zip(products['Price'], products['Quantity']) to multiply each pair.
3
Remove the Quantity column
Remove the 'Quantity' key and its values from the products dictionary using the del statement.
Data Analysis Python
Hint
Use del products['Quantity'] to remove the column.
4
Print the final products dictionary
Print the products dictionary to display the updated table with the 'Name', 'Price', and 'TotalValue' columns.
Data Analysis Python
Hint
Use print(products) to show the final dictionary.
Practice
(1/5)
1. What is the correct way to add a new column named 'age' with all values set to 30 in a pandas DataFrame df?
easy
A. df.append('age', 30)
B. df.add_column('age', 30)
C. df['age'] = 30
D. df.insert_column('age', 30)
Solution
Step 1: Understand how to add columns in pandas
In pandas, you add a new column by assigning a value or list to df['column_name'].
Step 2: Apply the correct syntax to add a column named 'age'
Using df['age'] = 30 adds a new column 'age' with all values set to 30.
Final Answer:
df['age'] = 30 -> Option C
Quick Check:
Adding column = df['new_col'] = value [OK]
Hint: Use df['col_name'] = value to add columns fast [OK]
Common Mistakes:
Using non-existent methods like add_column or insert_column
Trying to use append which is for rows, not columns
Forgetting to use square brackets for column name
2. Which of the following is the correct syntax to remove a column named 'salary' from a pandas DataFrame df?
easy
A. df.drop('salary', axis=1)
B. df.remove('salary')
C. df.delete_column('salary')
D. df.drop_column('salary')
Solution
Step 1: Recall pandas method to remove columns
The pandas method to remove columns is drop() with axis=1 to specify columns.
Step 2: Use correct syntax to drop 'salary' column
Using df.drop('salary', axis=1) removes the 'salary' column from the DataFrame.
Final Answer:
df.drop('salary', axis=1) -> Option A
Quick Check:
Remove column = df.drop('col', axis=1) [OK]
Hint: Use df.drop('col', axis=1) to remove columns [OK]
Common Mistakes:
Using methods like remove or delete_column which don't exist
Forgetting axis=1 to specify column removal
Using drop without parentheses or wrong parameters
3. Given the DataFrame df below:
name score
0 Amy 90
1 Bob 80
2 Cara 85
What will be the output of this code?
df['passed'] = df['score'] >= 85
print(df)
medium
A.
name score passed
0 Amy 90 False
1 Bob 80 True
2 Cara 85 False
B.
name score passed
0 Amy 90 True
1 Bob 80 False
2 Cara 85 True
C.
name score
0 Amy 90
1 Bob 80
2 Cara 85
D.
name score passed
0 Amy 90 90
1 Bob 80 80
2 Cara 85 85
Solution
Step 1: Understand the condition applied to 'score'
The expression df['score'] >= 85 creates a boolean series: True where score is 85 or more, False otherwise.
Step 2: Assign boolean series to new column 'passed'
Adding df['passed'] = ... adds this boolean series as a new column.
Final Answer:
DataFrame with 'passed' column showing True for scores >= 85 -> Option B
Quick Check:
Boolean condition adds True/False column [OK]
Hint: Compare column with condition to add True/False column [OK]
Common Mistakes:
Expecting numeric values instead of booleans in new column
Not using >= operator correctly
Printing df before adding the new column
4. What is wrong with this code snippet to remove the column 'height' from DataFrame df?
df.drop('height')
medium
A. Missing axis=1 to specify column removal
B. The column name should be in a list like ['height']
C. The method drop does not exist in pandas
D. You must assign the result back to df like df = df.drop('height')
Solution
Step 1: Check the drop method parameters
By default, drop() removes rows. To remove columns, you must specify axis=1.
Step 2: Identify the missing axis parameter
The code misses axis=1, so it tries to remove a row named 'height', causing an error if no such row exists.
Final Answer:
Missing axis=1 to specify column removal -> Option A
Quick Check:
drop columns = drop('col', axis=1) [OK]
Hint: Always add axis=1 to drop columns, not rows [OK]
Common Mistakes:
Forgetting axis=1 causes wrong row removal or error
Thinking drop modifies df inplace without assignment
Using wrong data type for column name
5. You have a DataFrame df with columns ['A', 'B', 'C']. You want to create a new column 'D' which is the sum of 'A' and 'B', then remove column 'C'. Which code correctly does this in one step?