Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter rows based on comparing another column's value

I want to produce a new dataframe from the old dataframe.

>>> old_df
   id name  value
0  aa    A     10
1  bb    B     20
2  aa    C      7
3  cc    D     30
4  aa    E     25
5  bb    F     12
6  dd    G    100

I only want those rows where value is lowest.

>>> new_df
   id name  value
0  aa    C      7
1  bb    F     12
2  cc    D     30
3  dd    G    100

Can anyone help me??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This will find the min of each id based on the value column

df.loc[df.groupby('id')['value'].idxmin()]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading