Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting multiple (neighboring) rows conditionally

I'd like to return the rows which qualify to a certain condition. I can do this for a single row, but I need this for multiple rows combined. For example 'light green' qualifies to 'XYZ' being positive and 'total' > 10, where 'Red' does not. When I combine a neighbouring row or rows, it does => 'dark green'. Can I achieve this going over all the rows and not return duplicate rows?

N = 1000

np.random.seed(0)

df = pd.DataFrame(
    {'X':np.random.uniform(-3,10,N),
     'Y':np.random.uniform(-3,10,N),
     'Z':np.random.uniform(-3,10,N),
    })

df['total'] = df.X + df.Y + df.Z

df.head(10)

enter image description here

EDIT;

Desired output is 'XYZ'> 0 and 'total' > 10

like image 595
Zanshin Avatar asked Mar 17 '26 23:03

Zanshin


1 Answers

Here's a try. You would maybe want to use rolling or expanding (for speed and elegance) instead of explicitly looping with range, but I did it that way so as to be able to print out the rows being used to calculate each boolean.

df = df[['X','Y','Z']]    # remove the "total" column in order
                          # to make the syntax a little cleaner

df = df.head(4)           # keep the example more manageable

for i in range(len(df)):
    for k in range( i+1, len(df)+1 ):
        df_sum = df[i:k].sum()
        print( "rows", i, "to", k, (df_sum>0).all() & (df_sum.sum()>10) )

rows 0 to 1 True
rows 0 to 2 True
rows 0 to 3 True
rows 0 to 4 True
rows 1 to 2 False
rows 1 to 3 True
rows 1 to 4 True
rows 2 to 3 True
rows 2 to 4 True
rows 3 to 4 True
like image 120
JohnE Avatar answered Mar 20 '26 13:03

JohnE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!