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 print the three rows with the highest values in a single column in a pandas dataframe

I have a pd.dataframe that look like this:

    0   1
0   10  0.9679487179487178
1   38  0.9692307692307693
2   24  0.9833333333333332
3   62  0.9525641025641025
4   17  0.9679487179487178
5   23  0.9679487179487178
6   72  0.9679487179487178
7   22  0.9538461538461538
8   90  0.9525641025641025
9   32  0.9666666666666668

How can I ask python to print out something like this:
"Highest accuracy was 0.9833333333333332 using 24 features, second highest accuracy
was 0.9692307692307693 with 38 features, third highest accuracy was at 0.9679487179487178
with 10 features"

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 :

If you sort your dataframe by that accuracy like so:

df.sort_values(by=["1"], inplace=True)

And you want the three highest accuracies you can do:

for i in range(3):
   print(f"Top {i+1} accuracy was {df["1"].loc[i]} with {df["0"].loc[i]} features")

If you want to print exactly what you wrote on the question then just replace {i+1} with {order[i]} and create order = ["Highest", "Second Highest", "Third Highest"]

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