Reputation: 374
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
Upvotes: 3
Views: 109
Reputation: 591
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
Upvotes: 1
Reputation: 22513
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
Upvotes: 2