Don Coder
Don Coder

Reputation: 556

Finding value in a range in a pandas dataframe

I have a dataframe and have a column named BOL. This value is between 2.0 and -2.0. I am trying to find, once BOL value reaches up to 0, if it reaches to 1.0 in 10 rows after current row. And would like to findout in how many rows BOL reaches to 1.0 after it's value 0.

Here is the dataframe:

import pandas as pd

df = pd.DataFrame({'BOL': [-1.21, -1.35, -1.1, -0.84, -0.01, 0.09, 
                           -0.45, 0.78, 1.45, 1.78, 0.33, -0.96]})

Upvotes: 1

Views: 69

Answers (2)

Anton vBR
Anton vBR

Reputation: 18916

Here is a different approach where we create a function that accepts an array and turns it into an iterator. If a >=0 value if found it will run for another 10 iterations looking for >=1 to return True. Else False.

import pandas as pd

df = pd.DataFrame({
    'BOL': [-1.21, -1.35, -1.1, -0.84, -0.01, 0.09, 
            -0.45, 0.78, 0.45, 0.78, 0.33, -0.96]
})


def findnextoccur(ar, n=10):
    it = np.nditer(ar)
    f = next(it)
    while f < 0:
        try:
            f = next(it)
        except StopIteration:
            return False
    for _ in range(n):
        try:
            f = next(it)
            if f >= 1.0:
                return True
        except StopIteration:
            return False

findnextoccur(df['BOL'])

Upvotes: 2

jpp
jpp

Reputation: 164773

This is one way with numpy:

import numpy as np

A = df['BOL'].values

start = np.where(A>=0)[0][0]  # 5
end = np.where(A[start:]>=1)[0][0] + start  # 8

res = end - start  # 3

If you expect a result, but it is possible that there are no valid start or end indices, then you can wrap with try / except:

try:
    start = np.where(A>=0)[0][0]
    end = np.where(A[start:]>=1)[0][0] + start
    res = end - start
except IndexError:
    res = np.nan

You can then perform a simple comparison, i.e. res <= 10.

Upvotes: 1

Related Questions