Python regex match groups and replace

Question:

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]))

Asked By: matt.aurelio

||

Answers:

We can use str.replace as follows:

df["period"] = df["period"].str.replace(r'^(d{2})_(d{4})$', r'2.1', regex=True)
Answered By: Tim Biegeleisen

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
Answered By: PaulS
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.