pandas dataframe look ahead optimization <p>What's the best way to do this with pandas dataframe? I want to loop through a dataframe, and find the nearest next index that has at least +/-2 value difference. For example: [100, 99, 102, 98, 103, 103] will create a new column with this [2, 2, 3, 0, N/A], 0 means not find.</p> <p>My solution performance is n * log(n). Any brilliant people could please show me a better performance solution?</p>