Back to blog
Mar 02, 2024
8 min read

Rename Columns

Rename DataFrame columns from snake_case to Title Case format

Difficulty: Easy | Acceptance: 85.20% | Paid: No Topics: N/A

Problem Description

DataFrame students

+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| student_id  | int    |
| name        | object |
| age         | int    |
+-------------+--------+

Write a solution to rename the columns as follows:

  • student_id → Student Id
  • name → Name
  • age → Age

The output should look like this:

+------------+--------+
| Student Id | Name   | Age   |
+------------+--------+
| 1          | Alice  | 20    |
| 2          | Bob    | 21    |
+------------+--------+

Table of Contents

Examples

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.

Constraints

- 1 <= number of columns <= 100
- 1 <= number of rows <= 1000
- Column names consist of lowercase letters and underscores only
- Column names start and end with a letter (not underscore)
- No consecutive underscores in column names

String Manipulation Approach

Intuition Split each column name by underscores, capitalize each word, and join them back with spaces.

Steps

  • Get the list of column names from the DataFrame
  • For each column name, split by underscore
  • Capitalize the first letter of each word
  • Join the words with spaces
  • Assign the new column names to the DataFrame
python
import pandas as pd

def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
    new_columns = []
    for col in students.columns:
        words = col.split('_')
        title_case = ' '.join(word.capitalize() for word in words)
        new_columns.append(title_case)
    students.columns = new_columns
    return students

Complexity

  • Time: O(n × m) where n is the number of columns and m is the average length of column names
  • Space: O(n) for storing new column names
  • Notes: Simple and readable approach

List Comprehension Approach

Intuition Use list comprehension to transform all column names in a single line of code.

Steps

  • Get the list of column names
  • Apply transformation using list comprehension
  • Assign the result back to DataFrame columns
python
import pandas as pd

def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
    students.columns = [' '.join(word.capitalize() for word in col.split('_')) 
                        for col in students.columns]
    return students

Complexity

  • Time: O(n × m) where n is the number of columns and m is the average length of column names
  • Space: O(n) for storing new column names
  • Notes: More concise Python code using list comprehension

Regex Approach

Intuition Use regular expressions to find and replace patterns in column names.

Steps

  • Use regex to find underscores followed by lowercase letters
  • Replace with space and uppercase letter
  • Capitalize the first letter of the column name
python
import pandas as pd
import re

def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
    new_columns = []
    for col in students.columns:
        new_col = re.sub(r'_([a-z])', lambda m: ' ' + m.group(1).upper(), col)
        new_col = new_col[0].upper() + new_col[1:]
        new_columns.append(new_col)
    students.columns = new_columns
    return students

Complexity

  • Time: O(n × m) where n is the number of columns and m is the average length of column names
  • Space: O(n) for storing new column names
  • Notes: Regex approach is powerful but may be overkill for simple transformations