[Python] Get rows from a DataFrame by using a list of slices

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a several million row data frame, and a list of interesting sections I need to select out of it. I'm looking for a highly efficient (read as: fastest possible) way of doing this.

    I know I can do this:

    slices = [slice(0,10), slice(20,50), slice(1000,5000)]
    for slice in slices:
    df.loc[slice, 'somecolumn'] = True


    ... but that just seems like an inefficient way of getting the job done. It's really slow.

    This seems faster than the for loop above, but I'm not sure if this is the best possible approach:

    from itertools import chain
    ranges = chain.from_iterable(slices)
    df.loc[ranges, 'somecolumns'] = True


    This also doesn't work, even though it seems that maybe it should:

    df.loc[slices, 'somecolumns'] = True

    TypeError: unhashable type: 'slice'


    My primary concern in this is performance. I need the best I can get out of this due to the size of the data frames I am dealing with.

    Continue reading...

Compartilhe esta Página