0
0
Pandasdata~20 mins

Renaming columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 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.