[Python] Write expression to pickup specific content from a column using pandas

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I think I may be asking for the impossible. I am trying to pull the following from the Notes column and remove all words left over. The format I wish to grab is content that looks like '0-0-0' or '0 - 0 - 0'. But the expression I wrote is only grabbing row 5 & 6. I'll be eternally grateful.

    import pandas as pd
    import re as re

    data = {'Notes': ['1,088 errors - 0 comments - 1109 pages', '76-0-143-SIS',
    '0 - 18 - 624 *flashcard questions (119 pgs PDF)',
    '0-0-1 page', '3 - 8 - 15 M1', '0 - 6 - 115', '35-0-100']}

    QA_data = pd.DataFrame (data, columns = ['Notes'])

    print (QA_data)


    And it turns out to be like the following:

    Notes
    0 1,088 errors - 0 comments - 1109 pages
    1 76-0-143-SIS
    2 0 - 18 - 624 *flashcard questions (119 pgs PDF)
    3 0-0-1 page
    4 3 - 8 - 15 M1
    5 0 - 6 - 115
    6 35-0-100


    I used regex to build my expressions and am using pandas to pull the necessary information from a string.

    space = re.compile('^\d+ - \d+ - \d+$')
    nospace = re.compile('^\d+-\d+-\d+$')
    print(checker.match("123 -123 - 123"))


    Used a for loop to match my strings and replace everything else with ''.

    task_dicts = QA_data.to_dict()
    #print(task_dicts.keys())
    #print(task_dicts["Notes"])
    for keys in task_dicts["Notes"]:
    if not space.match(str(task_dicts["Notes"][keys])) and not space.match(str(task_dicts["Notes"][keys])):
    task_dicts["Notes"][keys] = ''

    for keys in task_dicts["Notes"]:
    print(task_dicts["Notes"][keys])

    Continue reading...

Compartilhe esta Página