I have a pandas dataframe given below
id val ulim llim
1 100.25 101 98
2 97.30 99 95
3 104.22 106 100
4 105.00 107 102
5 95.00 99 91
.. .... .. ..
100000 105.92 107 103
For each row, I need to find if upper limit(ulim) has been reached first or the lower limit(llim) has been reached first.
For example:
For the first row, the Value (val) is 100.25 , the upper limit is 101 and the lower limit is 98.
The value of the second row, 97.30 is lesser than the lower limit (llim) . Hence , I will mark this row as (-1).
For the second row, the value(val) is 97.30, the upper limit is 99 and lower limit is 95. The value of the third row, 104.22 is higher than the upper limit. Hence , this row will be marked as (1).
For the third row, the value(val) is 104.22. The upper limit is 106 and the lower limit is 100. The value in the fourth row (105) is in between upper limit and lower limit. Hence, we will move to the the fifth row where the value is 95 and its below the lower limit(100). Hence, this row will be marked as -1.
Target df would be as follows
id val ulim llim result
1 100.25 101 98 -1
2 97.30 99 95 1
3 104.22 106 100 -1
4 105.00 107 102 -1
5 95.00 99 91 1
.. .... .. ..
100000 105.92 107 103 NaN
I have more than a million rows like this. Is it possible to have a solution without iteration?
The current iterative solution which I tried is very slow and is as follows
- Loop through each row in the data frame
- Find the row index and slice the data frame as df.iloc[current_index:]
- Take the val column from the sliced data frame and convert it to a list (sliced_df["val"].tolist())
- A list comprehension to check if the first upper limit was reached or the lower limit was reached.
- mark the result column based on step 4 result.