I have a big numeric Pandas dataframe df
, and I want to select out the rows whose certain column's value is within the range of min_value
and max_value
.
I can do this by:
filtered_df = df[(df[col_name].values >= min_value) & (df[col_name].values <= max_value)]
And I am looking for methods to speed it up . I try below:
df.sort(col_name, inplace=True)
left_idx = np.searchsorted(df[col_name].values, min_value, side='left')
right_idx = np.searchsorted(df[col_name].values, max_value, side='right')
filtered_df = df[left_idx:right_idx]
But it does not work for df.sort() costs more time.
So, any tips to speed up the selection ?
(Pandas 0.11)