Challenge - 5 Problems
Renaming Columns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of renaming columns with a dictionary
What is the output DataFrame after renaming columns using the given dictionary?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) new_df = df.rename(columns={'A': 'Alpha', 'B': 'Beta'}) print(new_df)
Attempts:
2 left
💡 Hint
Look at how the rename method uses the dictionary keys as old names and values as new names.
✗ Incorrect
The rename method changes column names based on the dictionary. 'A' becomes 'Alpha' and 'B' becomes 'Beta'.
❓ data_output
intermediate2:00remaining
Result of renaming columns with inplace=True
What is the DataFrame after renaming columns inplace?
Pandas
import pandas as pd df = pd.DataFrame({ 'X': [5, 6], 'Y': [7, 8] }) df.rename(columns={'X': 'x_coord', 'Y': 'y_coord'}, inplace=True) print(df)
Attempts:
2 left
💡 Hint
Using inplace=True changes the original DataFrame columns.
✗ Incorrect
With inplace=True, the original DataFrame columns are renamed directly to 'x_coord' and 'y_coord'.
🔧 Debug
advanced2:00remaining
Identify the error in renaming columns
What error does this code raise when trying to rename columns?
Pandas
import pandas as pd df = pd.DataFrame({ 'col1': [1, 2], 'col2': [3, 4] }) # Incorrect rename syntax new_df = df.rename(columns=['col1', 'new_col1']) print(new_df)
Attempts:
2 left
💡 Hint
Check the type of argument passed to columns parameter in rename.
✗ Incorrect
The rename method expects a dictionary or function for columns, not a list. Passing a list causes ValueError.
🚀 Application
advanced2:00remaining
Rename columns using a function
Which option correctly renames all columns to uppercase using a function?
Pandas
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bob'], 'age': [25, 30] }) new_df = df.rename(columns=??? ) print(new_df)
Attempts:
2 left
💡 Hint
The rename method can take a function to apply to each column name.
✗ Incorrect
Passing a function like lambda x: x.upper() renames all columns to uppercase.
🧠 Conceptual
expert3:00remaining
Effect of rename with copy=False
What is the effect of using rename with copy=False on the original DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({ 'a': [1, 2], 'b': [3, 4] }) new_df = df.rename(columns={'a': 'alpha'}, copy=False) new_df.loc[0, 'alpha'] = 100 print(df)
Attempts:
2 left
💡 Hint
copy=False means no new DataFrame is created, changes affect original.
✗ Incorrect
With copy=False, rename returns a view on the original DataFrame. Changes to new_df affect df.