Say I have a dataframe animals
with a column called weight
. I want to perform several filtering operations of the form
mask = animals['weight'] == 123
animals_filtered = animals[mask]
or
mask = animals['weight'] <= 123
animals_filtered = animals[mask]
as efficiently as possible.
Would it help me in any way to preprocess my dataframe by sorting it according to the values in the weight
column?
animals.sort_values(by='weight', inplace=True)
If yes, how can I take advantage of the fact that my column is sorted afterwards when performing the filtering operations?