Challenge - 5 Problems
Inplace Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Effect of inplace=True on DataFrame drop
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) df.drop('A', axis=1, inplace=True) print(df)
Attempts:
2 left
💡 Hint
inplace=True modifies the original DataFrame directly.
✗ Incorrect
Using inplace=True drops column 'A' from df directly, so printing df shows only column 'B'.
❓ Predict Output
intermediate2:00remaining
Return value of inplace operation
What does the variable 'result' contain after running this code?
Pandas
import pandas as pd df = pd.DataFrame({'X': [10, 20, 30]}) result = df.drop('X', axis=1, inplace=True) print(result)
Attempts:
2 left
💡 Hint
inplace operations return None.
✗ Incorrect
When inplace=True is used, the method returns None instead of a new DataFrame.
❓ data_output
advanced2:30remaining
Effect of inplace=False on DataFrame sort
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'A': [3, 1, 2]}) sorted_df = df.sort_values('A', inplace=False) print(df) print(sorted_df)
Attempts:
2 left
💡 Hint
inplace=False returns a new sorted DataFrame, original stays same.
✗ Incorrect
df remains unsorted, sorted_df is a new DataFrame sorted by column 'A'.
🔧 Debug
advanced2:30remaining
Why does this inplace operation not change the DataFrame?
Consider this code:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3]})
df.drop('A', axis=1)
print(df)
Why does df still have column 'A' after drop?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) df.drop('A', axis=1) print(df)
Attempts:
2 left
💡 Hint
Check if drop modifies df or returns a new object.
✗ Incorrect
drop returns a new DataFrame by default; df remains unchanged unless inplace=True is used.
🚀 Application
expert3:00remaining
Choosing inplace vs non-inplace for chained operations
You want to clean a DataFrame by dropping column 'B' and then sorting by column 'A'. Which code snippet correctly performs both operations and keeps the changes?
Attempts:
2 left
💡 Hint
inplace=True modifies df directly; assigning the result of inplace=True methods sets df to None.
✗ Incorrect
Option C modifies df inplace for both operations, so df reflects both changes. Option C assigns None to df causing errors. Option C does not modify df. Option C correctly assigns new DataFrames but is not the inplace approach.