Home > OS >  How to print the three rows with the highest values in a single column in a pandas dataframe
How to print the three rows with the highest values in a single column in a pandas dataframe

Time:06-22

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"

CodePudding user response:

You can use pandas.DataFrame.nlargest

out = df.nlargest(3, '1')
print(out)

    0         1
2  24  0.983333
1  38  0.969231
0  10  0.967949

CodePudding user response:

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

CodePudding user response:

sort the file with the acc colom somthing like this: data.sort_values("colom name", axis=0, ascending=True,inplace=True, na_position='first')

and then print what you want in order

  • Related