Taking rows from dataframe until a condition is met

Question:

I have a dataframe with two columns:

      A B
    0 1 3
    1 2 2
    2 3 2
    3 9 3 
    4 1 1 
    ...

For a given index i, I want the rows from row i to the row j in which df.at[j,A]-df.at[i,B]>5. I don’t want any rows after row j.

For example, let i=1, the output should be:

[out]
A B
2 2
3 2
9 3

Is there a simple way of do this without using loops?

Asked By: High GPA

||

Answers:

df = pd.DataFrame({'A': [10, 1, 2, 3, 9], 'B': [1, 3, 2, 2, 3]})
i = 2
base = df.at[i, 'B']
df = df.iloc[i:]
j = df[df['A'] - df.at[i, 'B'] > 5]
if not j.empty:
    print(df.iloc[:j.index[0]])
else:
    print('Condition not found')

Prints:

   A  B
2  2  2
3  3  2
4  9  3
Answered By: Алексей Р

You could try as follows:

import pandas as pd

data = {'A': {0: 10, 1: 2, 2: 3, 3: 9}, 'B': {0: 3, 1: 2, 2: 2, 3: 3}}
df = pd.DataFrame(data)

i=1
s = df.loc[i:,'A']-df.loc[i,'B']>5
trues = s[s==True]
if not trues.empty: 
    subset = df.iloc[i:trues.idxmax()+1]
else:
    subset = pd.DataFrame()
print(subset)

   A  B
1  2  2
2  3  2
3  9  3
Answered By: ouroboros1
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.