Using regex matched groups in pandas dataframe replace function

Learn, how to use regex matched groups in pandas dataframe replace function in Python? By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Regex matched groups in pandas dataframe replace function

Suppose we are given the dataframe and we want to use replace function on a column in this data frame and who rejects what we want to re-insert the parts of the match two groups.

Regex (Regular Expression)

A special format string is used for searching and filtering in pandas DataFrame rows.

Example:

  • 'K.*' : It will filter all the records which start with the letter 'K'.
  • 'A.*' : It will filter all the records which start with the letter 'A'.

To replace a value using regex or by comparing the value by regex, we will use "df[col].str.replce() method, inside which we will define our regex to compare.

Let us understand with the help of an example,

Python program to demonstrate regex matched groups in pandas dataframe replace function

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating DataFrame
df = pd.DataFrame({'Name':['Raman,Pandey','Mohan,Bhargava']})

# Display Original dataframe
print("Original DataFrame:\n",df,"\n")

# Replacing values using regex
df['Name'].replace({r'(\w+),\s+(\w+)' : r'\2 \1', 'Max':'Fritz'}, regex=True) # Print result

print("Result:\n",df)

Output

The output of the above program is:

Example: Using regex matched groups in pandas dataframe replace function

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.