Home > other >  How to extract values from column to another two?
How to extract values from column to another two?

Time:05-24

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?

CodePudding user response:

You can try

df = pd.DataFrame({'column_name': {0: '0122', 1: '012022', 2: '01/22', 3: '9922', 4: '03/23'}})

df_filtered = pd.DataFrame(df['column_name'].apply(extractMMYY).tolist(), columns=['MM', 'YY'])
print(df_filtered)

     MM    YY
0    01    22
1    01  2022
2    01    22
3  None  None
4    03    23
  • Related