📖 Scenario: You work as a data analyst. You have a small sales dataset with product names and prices. You want to update the prices by applying a discount. You will learn how to do this with and without inplace operations in pandas.
🎯 Goal: Build a pandas DataFrame with product prices, create a discount rate variable, apply the discount to prices using inplace and non-inplace methods, and print the updated DataFrame.
📋 What You'll Learn
Create a pandas DataFrame called df with columns Product and Price with exact values
Create a variable discount with the value 0.1 (10% discount)
Apply the discount to the Price column using inplace operation
Print the updated DataFrame df
💡 Why This Matters
🌍 Real World
In real life, data analysts often need to update data values directly in their datasets. Understanding inplace operations helps save memory and write cleaner code.
💼 Career
Knowing how to use inplace operations in pandas is useful for data cleaning and transformation tasks in data science and analytics jobs.
Progress0 / 4 steps
1
Create the sales DataFrame
Import pandas as pd and create a DataFrame called df with these exact rows: Product as ['Pen', 'Notebook', 'Eraser'] and Price as [1.5, 3.0, 0.5].
Pandas
Hint
Use pd.DataFrame with a dictionary for columns.
2
Create the discount variable
Create a variable called discount and set it to 0.1 to represent a 10% discount.
Pandas
Hint
Just assign 0.1 to discount.
3
Apply discount using inplace operation
Use the df['Price'].mul() method with 1 - discount and inplace=True to update the Price column directly.
Pandas
Hint
Use the *= operator to update the column inplace.
4
Print the updated DataFrame
Print the DataFrame df to see the updated prices after the discount.
Pandas
Hint
Use print(df) to show the DataFrame.
Practice
(1/5)
1. What does setting inplace=True do in pandas methods?
easy
A. It modifies the original DataFrame directly without creating a copy.
B. It creates a new DataFrame and leaves the original unchanged.
C. It saves the DataFrame to a file automatically.
D. It prints the DataFrame to the console.
Solution
Step 1: Understand inplace parameter meaning
Setting inplace=True tells pandas to change the original data directly.
Step 2: Compare inplace with default behavior
By default, pandas methods return a new object and keep the original unchanged.
Final Answer:
It modifies the original DataFrame directly without creating a copy. -> Option A
Quick Check:
inplace=True means direct change [OK]
Hint: Inplace=True means change original data directly [OK]
Common Mistakes:
Thinking inplace=True creates a new copy
Confusing inplace with saving to file
Assuming inplace prints output
2. Which of the following is the correct syntax to drop a column named 'Age' from DataFrame df inplace?
easy
A. df.drop('Age', axis=1)
B. df.drop('Age', axis=1, inplace=True)
C. df.drop('Age', inplace=True)
D. df.drop('Age', axis=0, inplace=True)
Solution
Step 1: Identify correct axis for dropping columns
Columns are dropped with axis=1, rows with axis=0.
Step 2: Check syntax for inplace drop
To drop inplace, use inplace=True along with correct axis.
Final Answer:
df.drop('Age', axis=1, inplace=True) -> Option B
Quick Check:
Drop column inplace needs axis=1 and inplace=True [OK]
Hint: Drop columns inplace needs axis=1 and inplace=True [OK]