Rename Columns - Problem

You are given a DataFrame called students with the following structure:

Column NameType
idint
firstobject
lastobject
ageint

Your task is to rename the columns according to the following mapping:

  • idstudent_id
  • firstfirst_name
  • lastlast_name
  • ageage_in_years

Return the DataFrame with the renamed columns.

Input & Output

Example 1 — Basic DataFrame
$ Input: students = [{"id": 1, "first": "John", "last": "Doe", "age": 20}]
Output: [{"student_id": 1, "first_name": "John", "last_name": "Doe", "age_in_years": 20}]
💡 Note: All columns are renamed: id→student_id, first→first_name, last→last_name, age→age_in_years
Example 2 — Multiple Students
$ Input: students = [{"id": 1, "first": "Alice", "last": "Smith", "age": 19}, {"id": 2, "first": "Bob", "last": "Jones", "age": 21}]
Output: [{"student_id": 1, "first_name": "Alice", "last_name": "Smith", "age_in_years": 19}, {"student_id": 2, "first_name": "Bob", "last_name": "Jones", "age_in_years": 21}]
💡 Note: Same column renaming applied to all rows in the DataFrame
Example 3 — Empty DataFrame Structure
$ Input: students = []
Output: []
💡 Note: Empty DataFrame remains empty after column renaming operation

Constraints

  • DataFrame contains columns: id, first, last, age
  • Column renaming must follow the exact mapping specified
  • All data values remain unchanged, only column names are modified

Visualization

Tap to expand
Rename Columns - Dictionary Method INPUT DataFrame id first last age 1 John Doe 20 Original Columns: id first last age Data Types: id: int, first: object last: object, age: int students = [ {"id": 1, "first": "John"...} ] ALGORITHM STEPS 1 Create rename dict Map old to new names {"id": "student_id", "first": "first_name", "last": "last_name", "age": "age_in_years"} 2 Call .rename() Pass dict to columns param 3 DataFrame processes Maps each column name 4 Return result New DataFrame returned df.rename( columns=rename_dict ) # returns new df FINAL RESULT student_id first_name last_name age_in_years 1 John Doe 20 Renamed Columns: student_id first_name last_name age_in_years OK - Renamed! Data values unchanged Only column headers modified [{"student_id": 1, "first_name": "John"...}] Key Insight: The .rename(columns=dict) method creates a clean mapping from old to new column names. The dictionary keys are original names, values are new names. Data remains intact - only metadata changes. This approach is more readable and maintainable than directly modifying df.columns list. TutorialsPoint - Rename Columns | Dictionary Rename Method
Asked in
Meta 35 Netflix 28 Google 25
31.5K Views
Medium Frequency
~5 min Avg. Time
850 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