B. inplace=True is not a valid argument for rename.
C. The dictionary should be passed to the columns parameter.
D. The rename method cannot rename columns.
Solution
Step 1: Check rename method parameters
The rename method requires the dictionary to be passed with the keyword columns= to rename columns.
Step 2: Identify the error in the code
The code passes the dictionary as a positional argument without specifying columns=, so it will not rename columns.
Final Answer:
The dictionary should be passed to the columns parameter. -> Option C
Quick Check:
Use columns= to rename columns [OK]
Hint: Always use columns= when renaming columns [OK]
Common Mistakes:
Passing dictionary without columns= keyword
Confusing inplace argument
Thinking rename can't change columns
5. You have a DataFrame df with columns ['temp', 'humidity', 'pressure']. You want to rename only 'temp' to 'temperature' without changing other columns. Which code correctly does this and keeps the change in df?
hard
A. df.rename(columns={'temp': 'temperature'}, inplace=True)
B. df.columns = ['temperature', 'humidity', 'pressure', 'wind']
C. df.rename({'temp': 'temperature'})
D. df.rename(columns={'temp': 'temperature'})
Solution
Step 1: Identify correct rename syntax with inplace
To rename a single column and keep changes in the original DataFrame, use rename(columns={...}, inplace=True).
Step 2: Check other options
df.columns = ['temperature', 'humidity', 'pressure', 'wind'] replaces all columns, which is risky. Options C and D do not use inplace=True, so changes won't persist in df.
Final Answer:
df.rename(columns={'temp': 'temperature'}, inplace=True) -> Option A
Quick Check:
Use inplace=True to keep rename changes [OK]
Hint: Use inplace=True to save rename changes in original DataFrame [OK]
Common Mistakes:
Replacing all columns instead of renaming one
Forgetting inplace=True to save changes
Using rename without columns= keyword
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.