0
0
Data Analysis Pythondata~20 mins

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

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