Calling rename without inplace=True returns a new DataFrame with renamed columns.
Step 2: Check the printed columns
The new DataFrame df_renamed has columns renamed to 'Alpha' and 'Beta'.
Final Answer:
['Alpha', 'Beta'] -> Option A
Quick Check:
rename() returns new DataFrame with renamed columns [OK]
Hint: rename() returns new DataFrame unless inplace=True [OK]
Common Mistakes:
Assuming original df columns change without inplace=True
Expecting error without inplace=True
Confusing columns list output
4. What is wrong with this code snippet?
df.rename({'old': 'new'}, inplace=True)
medium
A. The 'inplace' parameter is not valid for rename
B. The rename method cannot rename columns
C. The dictionary keys and values should be lists, not strings
D. The dictionary should be passed to the 'columns' parameter
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
Passing the dictionary without 'columns=' causes pandas to treat it as index renaming, not columns.
Final Answer:
The dictionary should be passed to the 'columns' parameter -> Option D
Quick Check:
Use rename(columns=dict) to rename columns [OK]
Hint: Always use columns= when renaming columns [OK]
Common Mistakes:
Passing dictionary without columns= keyword
Confusing index renaming with column renaming
Thinking inplace=False causes error
5. You have a DataFrame df with columns ['name', 'age', 'city']. You want to rename 'name' to 'full_name' and 'city' to 'location' without changing 'age'. Which code correctly renames only these columns and keeps the rest unchanged?
hard
A. df.rename(columns=['full_name', 'age', 'location'], inplace=True)
B. df.columns = ['full_name', 'age', 'location']
C. df.rename(columns={'name': 'full_name', 'city': 'location'}, inplace=True)
D. df.rename({'name': 'full_name', 'city': 'location'}, inplace=True)
Solution
Step 1: Understand selective renaming
Using rename with a dictionary for columns changes only specified columns, leaving others unchanged.
Step 2: Check each option
df.rename(columns={'name': 'full_name', 'city': 'location'}, inplace=True) uses columns= with a dictionary for selective renaming, leaving 'age' unchanged. In contrast, assigning to df.columns replaces all column names and requires the list to match the exact current order. Passing the dictionary directly without columns= attempts to rename the index instead of columns. Using a list instead of a dictionary for columns= will raise a TypeError.
Final Answer:
df.rename(columns={'name': 'full_name', 'city': 'location'}, inplace=True) -> Option C
Quick Check:
Use rename(columns=dict) for selective renaming [OK]
Hint: Use rename(columns=dict) to rename selected columns only [OK]
Common Mistakes:
Replacing all columns instead of selective renaming
Omitting columns= keyword
Using list instead of dictionary for columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.