'How to extract values from column to another two?

I use Pandas first time. There is a regex rule that extract mm, yy from string 0122 or 012022 or 01/22:

def extractMMYY(str):
    pattern = r"^((?:0[1-9]|1[0-2])|[0-9])[.\/\\-]*([0-9]{3}[0-9]|[0-9]{2})$"
    match = re.search(pattern, str)
    mm = None
    yy = None

    if match:
        mm = match.group(1)
        yy = match.group(2)

        if mm and yy:
            return mm, yy
    return mm, yy

I have tried to apply this function for specific column and get a new dataframe:

df_filtered = df[df['column_name'].apply(extractMMYY)];

As result I need to create a two additional columns: MM, YY with values from extractMMYY.

How to do that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source