Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return values greater than but account for argument

Tags:

python

pandas

I want to return all values that are greater than a specific integer. However, I have a separate column that determines what greater than should be. Using the df below, if Direction is Right, then all rows where X is greater than mainX should be returned. If Direction is Left, then all rows less than X should be returned.

df = pd.DataFrame({
        'Time' : ['09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.1','09:00:00.2','09:00:00.2','09:00:00.2','09:00:00.2','09:00:00.2','09:00:00.2'],
        'Group' : ['I','J','I','J','I','J','I','J','I','J','I','J'],                  
        'Label' : ['A','B','C','D','E','F','A','B','C','D','E','F'],                 
        'X' : [8,4,3,8,7,4,7,3,3,4,6,1],
        'Y' : [3,6,4,8,5,2,8,8,2,4,5,1],
        'mainX' : [5,5,5,5,5,5,5,5,5,5,5,5],
        'mainY' : [5,5,5,5,5,5,5,5,5,5,5,5],
        'Direction' : ['Left','Right','Left','Right','Left','Right','Left','Right','Left','Right','Left','Right']
    })

def greater(df):

    for val in df['Direction']:

        if val == 'Right':
            Forward = df[df['X'] > df['mainX']]

        elif val == 'Left':
            Forward = df[df['X'] < df['mainX']]

        else:
            continue

        return Forward

df1 = greater(df)

Out:

          Time Group Label  X  Y  mainX  mainY Direction
1   09:00:00.1     J     B  4  6      5      5     Right
2   09:00:00.1     I     C  3  4      5      5      Left
5   09:00:00.1     J     F  4  2      5      5     Right
7   09:00:00.2     J     B  3  8      5      5     Right
8   09:00:00.2     I     C  3  2      5      5      Left
9   09:00:00.2     J     D  4  4      5      5     Right
11  09:00:00.2     J     F  1  1      5      5     Right

Intended:

          Time Group Label  X  Y  mainX  mainY Direction
1   09:00:00.1     I     C  3  4      5      5      Left
2   09:00:00.1     J     D  8  8      5      5     Right
3   09:00:00.2     I     C  3  2      5      5      Left
like image 427
jonboy Avatar asked Jan 24 '26 12:01

jonboy


2 Answers

Setup your conditions and use loc:

cond1 = (df["Direction"].eq("Right"))&(df["X"]>df["mainX"])
cond2 = (df["Direction"].eq("Left"))&(df["X"]<df["mainX"])

print (df.loc[cond1|cond2])

#
         Time Group Label  X  Y  mainX  mainY Direction
2  09:00:00.1     I     C  3  4      5      5      Left
3  09:00:00.1     J     D  8  8      5      5     Right
8  09:00:00.2     I     C  3  2      5      5      Left
like image 196
Henry Yik Avatar answered Jan 27 '26 02:01

Henry Yik


Just use a boolean mask like the other answer, or query with pythonic conditions

df.query("(Direction == 'Right' and X > mainX) or (Direction == 'Left' and X < mainX)")

will output


         Time Group Label  X  Y  mainX  mainY Direction
2  09:00:00.1     I     C  3  4      5      5      Left
3  09:00:00.1     J     D  8  8      5      5     Right
8  09:00:00.2     I     C  3  2      5      5      Left
like image 24
ATL Avatar answered Jan 27 '26 01:01

ATL



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!