Pandas DataFrame: Renaming all columns to snake_case (slug style)

Updated: February 22, 2024 By: Guest Contributor Post a comment

Introduction

In data analysis and manipulation with Python, Pandas is often a go-to library due to its ease of use and powerful features. One common task you might need to perform is renaming the columns of a DataFrame to a more consistent format like snake_case, which is preferred for Python variable names and hence enhances readability and maintainability of the code. In this tutorial, we’ll explore various ways to rename all columns of a DataFrame to snake_case, covering methods ranging from basic to more advanced approaches.

Understanding DataFrame Columns

Before we start renaming columns, it’s crucial to understand what DataFrame columns are and how they are structured. Columns in a DataFrame are essentially the variables of the dataset, each holding data for a particular attribute. The column names are stored in an Index object, which allows for efficient operations and manipulations.

Basic Method: Using the .columns Attribute

The simplest way to rename columns to snake_case is by directly modifying the DataFrame.columns attribute. This requires understanding of Python string methods and list comprehensions.

import pandas as pd

df = pd.DataFrame({
    'FirstName': ['Alex', 'Brian', 'Charles'],
    'LastName': ['Smith', 'Jones', 'Brown'],
    'AGE': [25, 30, 35],
    'E-mail Address': ['[email protected]', '[email protected]', '[email protected]']
})

def to_snake_case(s):
    return ''.join(['_' + c.lower() if c.isupper() else c for c in s]).lstrip('_')

df.columns = [to_snake_case(c) for c in df.columns]

print(df)

Output: FirstName LastName AGE E-mail Address alex smith 25 [email protected] brian jones 30 [email protected] charles brown 35 [email protected]

This method is straightforward but manual and may be tedious for large DataFrames with many columns.

Using rename Method with a Function

The rename method allows for more flexibility and can accept a function that iterates over each column name, allowing us to apply the snake_case conversion more systematically.

df.rename(columns=lambda x: to_snake_case(x), inplace=True)

Advanced: Regular Expressions and str.replace

For a more sophisticated approach, we can use regular expressions to identify and transform uppercase letters and spaces. This is particularly useful for more complex naming conventions.

import re
df.columns = df.columns.str.replace(r'([A-Z])', r'_\1').str.lower().str.lstrip('_').str.replace(r'\s+', '_')

Notice how the regular expression identifies uppercase letters and prefixes them with an underscore, then converts everything to lowercase, removes leading underscores, and replaces spaces with underscores.

Utilizing str.casefold for Unicode Compatibility

When dealing with non-ASCII characters, it’s important to use methods that are Unicode compatible. str.casefold is an excellent choice for this, making sure your column renaming works across different languages and scripts.

df.columns = [to_snake_case(c).casefold() for c in df.columns]

Conclusion

Renaming DataFrame columns to snake_case is a common task that can significantly improve the readability and maintainability of your data. We have explored multiple methods from basic direct manipulation to more advanced techniques involving regular expressions and Unicode compatibility. Choosing the right approach depends on the specific requirements of your project, the complexity of your column names, and the need for Unicode support. With the methods discussed, you should be able to handle most scenarios efficiently and effectively.