Rename Columns - Solution & Explanation
Problem Statement
DataFrame students
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| id | int |
| first | object |
| last | object |
| age | int |
+-------------+--------+
Write a solution to rename the columns as follows:
idtostudent_idfirsttofirst_namelasttolast_nameagetoage_in_years
The result format is in the following example.
Example 1: Input: +----+---------+----------+-----+ | id | first | last | age | +----+---------+----------+-----+ | 1 | Mason | King | 6 | | 2 | Ava | Wright | 7 | | 3 | Taylor | Hall | 16 | | 4 | Georgia | Thompson | 18 | | 5 | Thomas | Moore | 10 | +----+---------+----------+-----+ Output: +------------+------------+-----------+--------------+ | student_id | first_name | last_name | age_in_years | +------------+------------+-----------+--------------+ | 1 | Mason | King | 6 | | 2 | Ava | Wright | 7 | | 3 | Taylor | Hall | 16 | | 4 | Georgia | Thompson | 18 | | 5 | Thomas | Moore | 10 | +------------+------------+-----------+--------------+ Explanation: The column names are changed accordingly.
Approach Overview
Problem Overview: You receive a table-like data structure (typically a DataFrame) and a list of new column names. The task is to rename the existing columns so they match the provided list while preserving the underlying data.
Approach 1: Direct Column Assignment (O(n) time, O(1) extra space)
The most straightforward approach updates the column metadata directly. Iterate through the column list or assign the new column list to the table's column property. In environments like Python DataFrames or similar structures, setting df.columns = new_names immediately replaces the existing column labels. The operation touches each column once, so the time complexity is O(n) where n is the number of columns, while space remains O(1) because only metadata changes. This method is ideal when the number of new names exactly matches the current column count and no conditional logic is required.
Approach 2: Library Rename Utility (O(n) time, O(n) space)
Many data-processing libraries provide a built-in rename utility that accepts a mapping between old and new column names. You construct a dictionary or map where keys represent existing column labels and values represent their replacements, then call the library helper (for example rename()). Internally the library iterates over the column list and applies the mapping. The traversal takes O(n) time and the mapping structure requires O(n) extra space. This approach is preferred when only some columns change or when you want safer transformations with explicit mappings.
Both approaches rely on basic dataframe metadata manipulation rather than complex algorithms. The key operation is iterating across column identifiers and updating their labels. In some implementations, column names are stored in an array-like structure or managed through a hash map for quick lookup during rename operations.
Recommended for interviews: Direct column reassignment is typically the cleanest and most efficient solution when the problem guarantees the new name list matches the column count. It demonstrates you understand how column metadata works internally. The mapping-based rename utility is useful when partial renaming is required and shows familiarity with built-in library features. Interviewers usually expect the O(n) traversal approach because every column must be processed at least once.
Approach 1: Data Structure Manipulation
This approach focuses on manipulating the data structures that hold the DataFrame or its equivalent to rename the columns. The idea is to access the data structure directly and modify the column headers.
In Python, the Pandas library provides a rename function which can easily rename columns. The rename method takes a dictionary where keys are existing column names and values are the new names.
Code
Python
JavaScript
Complexity
Time Complexity: O(1) as renaming columns in a DataFrame does not depend on the number of rows.
Space Complexity: O(1), no additional space required beyond the rename operation.
Approach 2: Library Specific Utility
This approach relies on using specific libraries available for each programming language that are geared towards data manipulation. These libraries often offer built-in functionalities for renaming columns effortlessly.
In C#, the DataTable structure from System.Data namespace is used to represent the data. To rename columns, we update the ColumnName property of the appropriate columns in the DataTable.Columns collection.
Complexity
Time Complexity: O(1) since column renaming is independent of the data in the table.
Space Complexity: O(1) because it modifies column names in-place.
Approach 3: Default Approach
Code
Python
Complexity Comparison
| Approach | Complexity |
|---|---|
| Data Structure Manipulation | Time Complexity: O(1) as renaming columns in a DataFrame does not depend on the number of rows. |
| Library Specific Utility | Time Complexity: O(1) since column renaming is independent of the data in the table. |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Column Assignment | O(n) | O(1) | When replacing all column names with a new ordered list |
| Library Rename Utility (Mapping) | O(n) | O(n) | When renaming only specific columns or using explicit old-to-new mappings |
Video Solution
2885. Rename Columns | LeetCode | Python | Pandas • You Data And AI • 610 views views
Watch 8 more video solutions →Frequently Asked Questions
Is Rename Columns easy or hard?
Rename Columns Python/Java solution
How to solve Rename Columns in O(n)?
What is the best approach for Rename Columns?
Is Rename Columns asked at Google/Amazon/Meta?
What data structure is used in Rename Columns?
What is the time complexity of Rename Columns?
Ready to solve this problem?
Practice Rename Columns with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor