[Python] return if list is consecutive within python col

Discussão em 'Python' iniciado por Stack, Novembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I've got a column within a df labelled a. It contains a list of int values. I want to return if they are consecutive or not.

    I can do it passing in a single list but I want to iterate over each row.

    df = pd.DataFrame({'a': [[0,2], [9,11,12], [0,1,2], [10,11,13]]})

    def cons(L):
    return all(n-i == L[0] for i,n in enumerate(L))

    print(cons(df['a'][0])) # works

    df['cons'] = df['a'].apply(cons, axis=1) # error


    intended:

    a cons
    0 [0, 2] False
    1 [9, 11, 12] False
    2 [0, 1, 2] True
    3 [10, 11, 13] False

    Continue reading...

Compartilhe esta Página