Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Renaming columns in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Column Renaming 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?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2],
    'B': [3, 4],
    'C': [5, 6]
})

new_df = df.rename(columns={'A': 'X', 'B': 'Y'})
print(new_df)
A
   X  Y  C
0  1  3  5
1  2  4  6
B
   A  B  C
0  1  3  5
1  2  4  6
C
   X  B  C
0  1  3  5
1  2  4  6
D
   X  Y
0  1  3
1  2  4
Attempts:
2 left
💡 Hint
Check which columns are renamed and which remain unchanged.
❓ data_output
intermediate
2:00remaining
Result of renaming columns with inplace=True
What is the DataFrame after renaming columns with inplace=True?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'one': [10, 20],
    'two': [30, 40]
})
df.rename(columns={'one': 'first', 'two': 'second'}, inplace=True)
print(df)
A
   one  two
0   10   30
1   20   40
B
   first  second
0     10      30
1     20      40
C
   first  two
0     10    30
1     20    40
D
   one  second
0   10     30
1   20     40
Attempts:
2 left
💡 Hint
inplace=True changes the original DataFrame.
🔧 Debug
advanced
2:00remaining
Identify the error in renaming columns code
What error does this code raise when trying to rename columns?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
new_df = df.rename(columns=['A', 'B'])
ATypeError: unhashable type: 'list'
BNo error, code runs successfully
CTypeError: rename() got an unexpected keyword argument 'columns' with list
DTypeError: rename() argument 'columns' must be a dict or callable
Attempts:
2 left
💡 Hint
Check the expected type for the columns parameter in rename.
🚀 Application
advanced
2:00remaining
Rename columns using a function
Which option correctly renames all columns to uppercase using a function?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'name': ['Alice', 'Bob'],
    'age': [25, 30]
})

new_df = df.rename(columns=??? )
print(new_df)
Alambda x: x.upper()
B{col: col.upper() for col in df.columns}
Cstr.upper
Dmap(str.upper, df.columns)
Attempts:
2 left
💡 Hint
The columns parameter can accept a function applied to each column name.
🧠 Conceptual
expert
2:00remaining
Effect of renaming columns on DataFrame indexing
After renaming columns in a DataFrame, which statement is true about the DataFrame's index?
ARenaming columns does not affect the DataFrame's index.
BRenaming columns resets the index to default integer index.
CRenaming columns changes the DataFrame's index values.
DRenaming columns removes the index entirely.
Attempts:
2 left
💡 Hint
Think about what columns and index represent separately.

Practice

(1/5)
1. What does the df.rename(columns={'old': 'new'}) function do in pandas?
easy
A. It sorts the DataFrame by the column 'old'.
B. It deletes the column named 'old' from the DataFrame.
C. It adds a new column named 'new' with values from 'old'.
D. It changes the column name from 'old' to 'new' in the DataFrame.

Solution

  1. Step 1: Understand the rename function

    The rename method changes column names based on a dictionary mapping old names to new names.
  2. Step 2: Identify the effect of the dictionary

    The dictionary {'old': 'new'} means the column named 'old' will be renamed to 'new'.
  3. Final Answer:

    It changes the column name from 'old' to 'new' in the DataFrame. -> Option D
  4. Quick Check:

    Rename columns = change names [OK]
Hint: Rename columns using a dictionary with old:new pairs [OK]
Common Mistakes:
  • Thinking rename deletes columns
  • Confusing rename with adding columns
  • Assuming rename sorts data
2. Which of the following is the correct syntax to rename column 'A' to 'Alpha' in a pandas DataFrame df?
easy
A. df.rename('A' to 'Alpha')
B. df.rename(columns={'A': 'Alpha'}, inplace=True)
C. df.rename_columns('A', 'Alpha')
D. df.columns.rename('A', 'Alpha')

Solution

  1. Step 1: Recall pandas rename syntax

    The correct syntax uses df.rename(columns={old_name: new_name}, inplace=True) to rename columns.
  2. Step 2: Match the syntax to options

    df.rename(columns={'A': 'Alpha'}, inplace=True) matches the correct syntax exactly, including inplace=True to change the original DataFrame.
  3. Final Answer:

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

    Use rename with columns dict and inplace [OK]
Hint: Use rename(columns={old:new}, inplace=True) to rename columns [OK]
Common Mistakes:
  • Using wrong method names like rename_columns
  • Missing columns= keyword
  • Forgetting inplace=True to apply changes
3. Given the DataFrame df below:
import pandas as pd
df = pd.DataFrame({'X': [1,2], 'Y': [3,4]})
df.rename(columns={'X': 'A', 'Y': 'B'}, inplace=True)
print(df.columns.tolist())

What is the output?
medium
A. ['A', 'B']
B. ['X', 'Y']
C. ['X', 'B']
D. ['A', 'Y']

Solution

  1. Step 1: Understand the rename operation

    The rename changes 'X' to 'A' and 'Y' to 'B' in the DataFrame columns.
  2. Step 2: Check inplace=True effect

    Since inplace=True, the original DataFrame columns are updated.
  3. Final Answer:

    ['A', 'B'] -> Option A
  4. Quick Check:

    Renamed columns list = ['A', 'B'] [OK]
Hint: inplace=True updates columns immediately [OK]
Common Mistakes:
  • Ignoring inplace=True and expecting old names
  • Partial renaming confusion
  • Printing DataFrame instead of columns
4. What is wrong with this code snippet?
df.rename({'old_name': 'new_name'}, inplace=True)
medium
A. The dictionary keys and values are reversed.
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

  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

    The code passes the dictionary as a positional argument without specifying columns=, so it will not rename columns.
  3. Final Answer:

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

  1. 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).
  2. 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.
  3. Final Answer:

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

10 questions~5 mineasy hard