Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a pandas dataframe row where column has minimum value

I'm trying to select a row in Pandas DatFrame where a column has the lowest value. There should have an easy way of doing that, but so far I didn't find.

Suppose this dataframe:

>>> print(df.head())
    N   M  S
0  10  42  4
1  39  22  2
2  11  52  4
3  97  42  2
4  66  72  1

How do I get the row where a column has the minimum value? For example, how do I get the row where column 'S' has value 1?

like image 282
Heitor Avatar asked Dec 11 '22 01:12

Heitor


1 Answers

A column name of df.S works but a column name of df.T doesn't work. df.T invokes the Transpose operation because it takes namespace precedence. Here's @ansev answer using literals instead.

df[df['S']==df['S'].min()]
like image 167
BSalita Avatar answered Mar 01 '23 22:03

BSalita