Bird
Raised Fist0
Pandasdata~20 mins

Renaming columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Renaming Columns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
   Alpha  Beta
0      1     3
1      2     4
B
   A  B
0  1  3
1  2  4
C
   Alpha  B
0      1  3
1      2  4
D
   A  Beta
0  1     3
1  2     4
Attempts:
2 left
💡 Hint
Look at how the rename method uses the dictionary keys as old names and values as new names.
❓ data_output
intermediate
2: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)
A
   X  y_coord
0  5        7
1  6        8
B
   X  Y
0  5  7
1  6  8
C
   x_coord  Y
0        5  7
1        6  8
D
   x_coord  y_coord
0        5        7
1        6        8
Attempts:
2 left
💡 Hint
Using inplace=True changes the original DataFrame columns.
🔧 Debug
advanced
2: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)
AValueError: columns must be a dict or a function
BTypeError: rename() got an unexpected keyword argument 'columns' with list
CNo error, prints DataFrame with renamed columns
DTypeError: unhashable type: 'list'
Attempts:
2 left
💡 Hint
Check the type of argument passed to columns parameter in rename.
🚀 Application
advanced
2: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)
Alambda x: x.lower()
Blambda x: x.upper()
C{'name': 'NAME', 'age': 'AGE'}
D['NAME', 'AGE']
Attempts:
2 left
💡 Hint
The rename method can take a function to apply to each column name.
🧠 Conceptual
expert
3: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)
ARaises TypeError because copy=False is not allowed
BOriginal DataFrame df remains unchanged with columns 'a' and 'b'
COriginal DataFrame df is modified and shows 'a' column with updated value 100
DOriginal DataFrame df has columns 'alpha' and 'b' but values unchanged
Attempts:
2 left
💡 Hint
copy=False means no new DataFrame is created, changes affect original.

Practice

(1/5)
1. What is the main purpose of renaming columns in a pandas DataFrame?
easy
A. To delete unwanted columns
B. To make the data easier to read and understand
C. To sort the data by column values
D. To change the data type of columns

Solution

  1. Step 1: Understand the role of column names

    Column names help identify what data each column holds.
  2. Step 2: Purpose of renaming

    Renaming columns improves clarity and makes the data easier to work with.
  3. Final Answer:

    To make the data easier to read and understand -> Option B
  4. Quick Check:

    Renaming columns = better readability [OK]
Hint: Renaming columns improves clarity and usability [OK]
Common Mistakes:
  • Confusing renaming with deleting columns
  • Thinking renaming changes data values
  • Assuming renaming sorts data
2. Which of the following is the correct syntax to rename columns in a pandas DataFrame df using a dictionary mapping old names to new names?
easy
A. df.rename(columns={'old_name': 'new_name'}, inplace=True)
B. df.rename({'old_name': 'new_name'}, axis=0)
C. df.rename(columns=['old_name', 'new_name'])
D. df.rename(columns='old_name:new_name')

Solution

  1. Step 1: Identify correct method parameters

    The rename method requires a dictionary for columns with keys as old names and values as new names.
  2. Step 2: Check syntax correctness

    df.rename(columns={'old_name': 'new_name'}, inplace=True) uses columns= with a dictionary and inplace=True, which is correct syntax.
  3. Final Answer:

    df.rename(columns={'old_name': 'new_name'}, inplace=True) -> Option A
  4. Quick Check:

    Use rename(columns=dict) with inplace=True [OK]
Hint: Use rename(columns={old:new}) with inplace=True to rename [OK]
Common Mistakes:
  • Omitting 'columns=' keyword
  • Passing list instead of dictionary
  • Using axis=0 for column renaming
3. Given the DataFrame df below:
import pandas as pd
df = pd.DataFrame({
  'A': [1, 2],
  'B': [3, 4]
})
df_renamed = df.rename(columns={'A': 'Alpha', 'B': 'Beta'})
print(df_renamed.columns.tolist())

What will be the output?
medium
A. ['Alpha', 'Beta']
B. ['A', 'B']
C. ['Alpha', 'B']
D. Error: rename requires inplace=True

Solution

  1. Step 1: Understand rename without inplace

    Calling rename without inplace=True returns a new DataFrame with renamed columns.
  2. Step 2: Check the printed columns

    The new DataFrame df_renamed has columns renamed to 'Alpha' and 'Beta'.
  3. Final Answer:

    ['Alpha', 'Beta'] -> Option A
  4. 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

  1. Step 1: Check rename method parameters

    The rename method requires the dictionary to be passed with the keyword 'columns=' to rename columns.
  2. Step 2: Identify the error in the code

    Passing the dictionary without 'columns=' causes pandas to treat it as index renaming, not columns.
  3. Final Answer:

    The dictionary should be passed to the 'columns' parameter -> Option D
  4. 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

  1. Step 1: Understand selective renaming

    Using rename with a dictionary for columns changes only specified columns, leaving others unchanged.
  2. 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.
  3. Final Answer:

    df.rename(columns={'name': 'full_name', 'city': 'location'}, inplace=True) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard