I'm trying to slice a dataframe based on list of values, how would I go about this?
Say I have an expression or a list l = [0,1,0,0,1,1,0,0,0,1]
How to return those rows in a dataframe, df
, when the corresponding value in the expression/list is 1? In this example, I would include rows where index is 1, 4, 5, and 9.
You can use masking here:
So we construct a boolean array with true and false. Every place where the array is True is a row we select.
Mind that we do not filter inplace. In order to retrieve the result, you have to assign the result to an (optionally different) variable:
Setup
Borrowed @ayhan's setup
Without
numpy
not the fastest, but it holds its own and is definitely the shortest.
Timing
Testing Code
Selecting using a list of Booleans is something
itertools.compress
does well.Given
Code
yet another "creative" approach:
or much better variant from @ayhan:
PS i've also borrowed @Ayhan's setup
Or maybe find the position of 1 in your
list
and slice from theDataframe
Convert the list to a boolean array and then use boolean indexing: