Rename Columns - Problem

You are given a pandas DataFrame containing student information with the following columns: id, first, last, and age.

Your task is to rename these columns to make them more descriptive and follow a consistent naming convention:

  • idstudent_id
  • firstfirst_name
  • lastlast_name
  • ageage_in_years

This is a common data preprocessing task when working with datasets that have abbreviated or unclear column names. Good column naming is crucial for data clarity and maintainability!

Input & Output

example_1.py — Basic Student Data
$ Input: students = pd.DataFrame({ 'id': [1, 2, 3], 'first': ['Alice', 'Bob', 'Charlie'], 'last': ['Johnson', 'Smith', 'Davis'], 'age': [20, 21, 19] })
Output: student_id first_name last_name age_in_years 0 1 Alice Johnson 20 1 2 Bob Smith 21 2 3 Charlie Davis 19
💡 Note: All four columns are successfully renamed according to the mapping: id→student_id, first→first_name, last→last_name, age→age_in_years
example_2.py — Single Student Record
$ Input: students = pd.DataFrame({ 'id': [1001], 'first': ['Emma'], 'last': ['Watson'], 'age': [22] })
Output: student_id first_name last_name age_in_years 0 1001 Emma Watson 22
💡 Note: Even with a single row, the column renaming works exactly the same way - only the headers change, data remains intact
example_3.py — Empty DataFrame Edge Case
$ Input: students = pd.DataFrame({ 'id': [], 'first': [], 'last': [], 'age': [] })
Output: Empty DataFrame Columns: [student_id, first_name, last_name, age_in_years] Index: []
💡 Note: Column renaming works even on empty DataFrames - the structure is preserved with new column names, just no data rows

Visualization

Tap to expand
Step 1: OriginalAbbreviated Columnsid | first | last | age1 | John | Doe | 202 | Jane | Smith| 213 | Bob | Wilson|194 | Alice | Brown | 22Step 2: MappingCreate Dictionary'id' → 'student_id''first' → 'first_name''last' → 'last_name''age' → 'age_in_years'Step 3: Applyrename() Methodstudent_id | first_namelast_name | age_in_yearsSame data content......new column headersResultClear Column NamesDescriptive headers✓ student_id✓ first_name✓ last_name✓ age_in_years💡 Key Insight: Efficiency🚀 O(1) Time: Only headers modified, no data movement💾 O(1) Space: No memory overhead from data copying📈 Scalable: Works efficiently even with millions of rowsBest Practice: Use pandas.DataFrame.rename(columns=mapping_dict)This built-in method is optimized for column renaming operationsClean, readable, efficient, and follows pandas conventions
Understanding the Visualization
1
Original DataFrame Structure
DataFrame with abbreviated column names that need clarification
2
Create Column Mapping
Define a dictionary mapping old names to more descriptive new names
3
Apply rename() Method
Use pandas built-in rename() function with the mapping dictionary
4
Headers Updated Efficiently
Only column headers change - data stays in place with no copying overhead
Key Takeaway
🎯 Key Insight: Column renaming is a metadata operation that only changes headers, making it extremely efficient for any DataFrame size.

Time & Space Complexity

Time Complexity
⏱️
O(1)

Only column headers are modified, data is not copied

n
2n
Linear Growth
Space Complexity
O(1)

No additional space needed, columns renamed in place

n
2n
Linear Space

Constraints

  • DataFrame will always have exactly 4 columns: id, first, last, age
  • Number of rows can be 0 ≤ n ≤ 103
  • Column names must be renamed exactly as specified
  • Data types and values should remain unchanged
Asked in
Meta 25 Google 20 Netflix 15 Amazon 12
28.5K Views
High Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen