[Python] Fix wrong spelling after a run of correct spellings in a sorted dataframe in pandas

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

  1. Stack

    Stack Membro Participativo

    I have a dataframe like this:

    Bevonce,2008,296853
    Beyonce,2007,1210744
    Beyonce,2007,1222003
    Beyonce,2007,1222003
    Beyonce,2007,1222007
    Beyoncel,2007,1222002
    Nicki Mina,2015,2717068
    Nicki Minaj,2015,2741567
    Nicki Minaj,2015,2741567
    Nicki Minaj,2015,2743565
    Nicki Minajl,2015,2744974
    Nicki Minal,2015,2741562
    Nickl Minaj,2015,2741867


    I want to clean it to:

    Beyonce,2008,296853
    Beyonce,2007,1210744
    Beyonce,2007,1222003
    Beyonce,2007,1222003
    Beyonce,2007,1222007
    Beyonce,2007,1222002
    Nicki Minaj,2015,2717068
    Nicki Minaj,2015,2741567
    Nicki Minaj,2015,2741567
    Nicki Minaj,2015,2743565
    Nicki Minaj,2015,2744974
    Nicki Minaj,2015,2741562
    Nicki Minaj,2015,2741867


    Note: the kind of errors are

    - single character wrong
    - single character more
    - single character less
    - or combination of two of the above


    And to avoid false positive, I am thinking of cleaning a string only if I find its variation after a run of at least three rows of identical first columns (and hence expected to be correct) and that run of identical values can be used as source of truth for the following or preceding mismatching string.

    As you can see in the example, Bevonce is the variation from the following 4 run of Beyonce and also Beyoncel is mismatching from 4 run of preceding Beyonce.

    Also you can assume my dataframe is always expected to be in sorted order of first column.

    Continue reading...

Compartilhe esta Página