How to filter one row, calculate range and find similar rows from it falling within that range in a dictionary with id as key and id's falling in that range as values using multiprocessing?
Suppose I have a data frame:
id val1 val2
1 10 20
2 9.5 19
3 100 200
4 9.3 19.2
5 96 196
6 99 198
7 103 202
8 140 280
For each id i, I will calculate:
upper_val1 = df[df.id==i].val1 * (1+0.1)
lower_val1 = df[df.id==i].val1 * (1-0.1)
upper_val2 = df[df.id==i].val2 * (1+0.1)
lower_val2 = df[df.id==i].val2 * (1-0.1)
Subset df:
sub_df = df[(df.val1<=upper_val1)&df.val1>=lower_val1)&(df.val1<=upper_val2)&df.val1>=lower_val2)
For whichever id, val1 lies between this range, that will be put in the dictionary. For eg. the output of this df will be:
{1:[2,4], 2:[1,4], 4:[1,2], 3:[5,6,7], 5:[3,6,7], 6:[3,5,7], 7:[3,5,6]}
I have a data frame with millions of records and this step should be repeated for each row, so how it can be done using multiprocessing?