Skip to content
Advertisement

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”

Advertisement

Answer

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"]

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement