Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to do that?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading