How to rename the columns of a pandas dataframe using regex?

by herminia_bruen , in category: Third Party Scripts , 7 days ago

How to rename the columns of a pandas dataframe using regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 6 days ago

@herminia_bruen 

You can rename the columns of a pandas dataframe using regex by using the rename() function along with a lambda function that applies the regex pattern to each column name. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import pandas as pd

# Create a sample dataframe
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C_123': [7, 8, 9]}
df = pd.DataFrame(data)

# Define a regex pattern to match column names
pattern = r'[^w]'  # this pattern matches any non-alphanumeric characters

# Rename the columns using regex
df = df.rename(columns=lambda x: re.sub(pattern, '_', x))

# Print the dataframe with renamed columns
print(df)


In this example, we create a sample dataframe with columns 'A', 'B', and 'C_123'. We define a regex pattern r'[^w]' that matches any non-alphanumeric characters. Then, we use the rename() function with a lambda function that applies the regex pattern to each column name. Finally, we print the dataframe with the renamed columns.