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

Indices of smallest n values over whole Pandas DF

I’m searching for an efficient way to extract the indices of the n smallest values over the whole data frame.

For example, given the following df with n = 2:

    colA    colB    colC
r1   33      75      22
r2    1      52      95
r3   71       7      68

I would like to get, in some form, the indices [(r2, colA), (r3, colB)] corresponding to the 2 smallest values over the whole df: 1 and 7.

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

The order between the indices is not important (The corresponding values may not be sorted).

Thanks!

>Solution :

nsmallest

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.nsmallest.html

import pandas as pd
df=pd.DataFrame({"colA":[33,1,71],"colB":[75,52,7],"colC":[22,95,68]})

df.apply(pd.Series.nsmallest, axis=1, n=1)

df.apply(pd.Series.nsmallest, axis=1, n=2)

enter image description here

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