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

Time:01-28

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!

CodePudding user response:

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
  •  Tags:  
  • Related