Home > Software engineering >  Python regex match groups and replace
Python regex match groups and replace

Time:08-11

I have a pandas dataframe with the following sample data


df = pd.DataFrame({'period': ['01_2022', '02_2022', '05_2023', '06_2024']})

I would like to have a table that reframes


df = pd.DataFrame({'period': ['2022.01', '2022.02, 2023.05', '2024.06']})

Is there a way to do this with regex in Python?

Thank you

This is my way without regex but would like to do it with regex:


(lambda row: ".".join(row.split('_')[::-1]))

CodePudding user response:

We can use str.replace as follows:

df["period"] = df["period"].str.replace(r'^(\d{2})_(\d{4})$', r'\2.\1', regex=True)

CodePudding user response:

Another possible solution, using pandas.apply and re.sub:

import pandas as pd
import re 

df['period'] = (
  df['period']
  .apply(lambda x: re.sub(r"(\d{2})_(\d{4})", r"\2.\1", x)))

Output:

0    2022.01
1    2022.02
2    2023.05
3    2024.06
Name: period, dtype: object
  • Related