I am trying to come up with a way to filter dataframe so that it contains only certain range of numbers that is needed for further processing. Below is an example dataframe
data_sample = [['part1', 234], ['part2', 224], ['part3', 214],['part4', 114],['part5', 1111],
['part6',1067],['part7',1034],['part8',1457],['part9', 789],['part10',1367],
['part11',467],['part12',367]
]
data_df = pd.DataFrame(data_sample, columns = ['partname', 'sbin'])
data_df['sbin'] = pd.to_numeric(data_df['sbin'], errors='coerce', downcast='integer')
With the above dataframe i want to filter such that any part with sbin in range [200-230] and [1000-1150] and [350-370] and [100-130] are removed.
I have a bigger dataframe with lot more ranges to be removed and hence need a faster way than using below command
data_df.loc[~( ((data_df.sbin >=200) & (data_df.sbin <= 230)) | ((data_df.sbin >=100) & (data_df.sbin <= 130)) | ((data_df.sbin >=350) & (data_df.sbin <= 370))| ((data_df.sbin >=1000) & (data_df.sbin <= 1150)))]
that produces output as below
partname sbin
0 part1 234
7 part8 1457
8 part9 789
9 part10 1367
10 part11 467
The above method requires lot of conditions and takes a long time, i would like to know if there is a better way using regex or some other python way that i am not aware off.
any help would be great