Sling Academy
Home/Pandas/Pandas: Remove special characters and whitespace from column names

Pandas: Remove special characters and whitespace from column names

Last updated: February 21, 2024

Introduction

When working with data in Python, the pandas library is a powerhouse tool that allows for efficient data manipulation and analysis. However, it’s not uncommon to encounter datasets with column names that include special characters and whitespace, which can make data processing challenging. This tutorial will guide you through various methods to clean column names in pandas, making your data clean, consistent, and easy to work with. We’ll start with basic examples and gradually move to more advanced scenarios.

Getting Started

Before diving into code examples, make sure you have pandas installed:

pip install pandas

Now, we’re ready to start cleaning some column names!

Basic Cleaning

The simplest way to remove whitespace and special characters from column names is to use the str.replace() method along with the rename() function provided by pandas. Here’s a simple example:

import pandas as pd

df = pd.DataFrame({"name with space": range(5), "@special*char#column": range(5)})

df.columns = df.columns.str.replace(" ", "").str.replace("[^\w]", "", regex=True)

print(df.head())

Output:

   namewithspace  specialcharcolumn
0              0                  0
1              1                  1
2              2                  2
3              3                  3
4              4                  4

This piece of code removes all instances of whitespace and any special character, retaining only alphanumeric characters and underscores in column names.

Using Regex for More Control

If you need more control over which special characters to remove, you can use regular expressions (regex). The following example demonstrates how to keep underscores but remove other special characters and all whitespaces:

import pandas as pd

df = pd.DataFrame({"name with space": range(5), "@special*char#column": range(5)})

df.columns = df.columns.str.replace("[^\w\s]", "", regex=True).str.replace(
    "\s+", "_", regex=True
)

print(df.head())

Output:

   name_with_space  specialcharcolumn
0                0                  0
1                1                  1
2                2                  2
3                3                  3
4                4                  4

This approach replaces spaces with underscores for better readability, while removing other special characters.

Applying Functions with rename()

For more complex column name cleaning, the rename() function is incredibly flexible. You can pass a function that operates on each column name. Here’s how you can utilize it:

import pandas as pd
import re

df = pd.DataFrame({"name with space": range(5), "@special*char#column": range(5)})


def clean_column_name(column_name):
    column_name = column_name.strip()
    column_name = re.sub(r"[^\w\s]", "", column_name)
    column_name = re.sub(r"\s+", "_", column_name)
    return column_name


df.rename(columns=clean_column_name, inplace=True)

print(df.columns.tolist())

Output:

['name_with_space', 'specialcharcolumn']

This code cleans each column name by stripping leading and trailing spaces, replacing interior spaces with underscores, and removing special characters, all in a neat, reusable function.

Advanced Strategies

When dealing with larger datasets or more complex scenarios, you might want to automate the cleaning process further. One way is to leverage the power of list comprehensions combined with regex:

import pandas as pd
import re

df = pd.DataFrame({"name with space": range(5), "@special*char#column": range(5)})


df.columns = [re.sub(r"[^\w\s]", "", col).replace(" ", "_") for col in df.columns]
print(df.head())

Output:

   name_with_space  specialcharcolumn
0                0                  0
1                1                  1
2                2                  2
3                3                  3
4                4                  4

This concise approach applies a uniform cleaning procedure to all column names with minimal code.

Dealing with Duplicates

After cleaning column names, you might end up with duplicates. Ensuring unique column names is crucial for avoiding confusion in your data analysis process. Here’s how to append a number to duplicate names:

cols = pd.Series(df.columns)
for dup in cols[cols.duplicated()].unique():
    cols[cols[cols == dup].index.values.tolist()] = [
        f"{dup}_{i}" if i != 0 else dup for i in range(sum(cols == dup))
    ]
df.columns = cols

print(df.columns.tolist())

Output:

['name with space', '@special*char#column']

This snippet checks for duplicates and appends an index to make them unique, preserving original column names as much as possible.

Conclusion

Removing special characters and whitespace from column names in pandas is essential for maintaining a clean and effective dataframe structure. Throughout this tutorial, we’ve explored several methods, from simple replacements to more advanced techniques and handling duplicates. By incorporating these practices into your data cleaning process, you’ll ensure that your data is not only easier to work with but also more compatible with a wide range of pandas functionalities and data visualisation tools.

Next Article: Pandas: How to read an online CSV file that requires authentication

Previous Article: Pandas DataFrame: Convert all string values to lower/upper case

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)