Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas if column id value greater

I have what i think is a simple quesiton, but I am not sure how to implement. I have the following dataframe:

ID    Value
1     100
2     250
3     300
4     400
5     600
7     800

I would like to look at 2 id's: 3 & 5 and then drop the one with the lower value. So i am assuming I would use something like the following code, but again, i am not sure how to implement,nor am i sure how to utilize the inequality to point towards the value while directing my function at a very specific pair of id's.

def ChooseGreater(x):
    if df['id'] == 3 > df['id'] ==5
        return del df['id']==5
    else:
        return del df['id']==3

Thank you!

like image 867
John Avatar asked Jun 08 '26 23:06

John


2 Answers

I think you can do:

df.drop(df.loc[df.ID.isin([3,5]),'Value'].idxmin(), inplace=True)
like image 189
sacuL Avatar answered Jun 11 '26 13:06

sacuL


Using Python's min

df.drop(min(df.query('ID in [3, 5]').index, key=df.Value.get))

   ID  Value
0   1    100
1   2    250
3   4    400
4   5    600
5   7    800

groupby and tail

df.sort_values('Value').groupby(df.ID.replace({3: 5})).tail(1)

   ID  Value
0   1    100
1   2    250
3   4    400
4   5    600
5   7    800
like image 43
piRSquared Avatar answered Jun 11 '26 12:06

piRSquared