user12345
user12345

Reputation: 509

Python: How to find nth minimum value from a dataframe column?

Have got a dataframe like below:

Store       Row_no
11          56
11          57
11          58
12          89
12          90
12          91
12          92

For each store need to get 3rd minimum value from Row_no. Expected output below.

Store       Row_no
11          58
12          91

have tried df.Row_no.nsmallest(3) but it works different. Any help will be appreciated. Thank You!

Upvotes: 2

Views: 122

Answers (1)

jezrael
jezrael

Reputation: 862511

Use DataFrame.sort_values with GroupBy.nth:

df = df.sort_values(['Store','Row_no']).groupby('Store', as_index=False).nth(2)
print (df)
   Store  Row_no
2     11      58
5     12      91

Upvotes: 2

Related Questions