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

Ranking data from highest to lowest

I want the program to get an input of 5 numbers which represent people. Then the program should rank the numbers from highest to lowest e.g(5=1, 1=5). I have been trying all day to figure out how to rank them but have no idea. New to programming, thank you very much.

number1 = []    
number2 = []    
number3 = []    
number4 = []
number5 = []

count = 0
    
while count < 1:
    a = int(input("enter score num 1"))
    number1.append(a)
    
    b = int(input("enter score num 2"))
    number2.append(b)
    
    c = int(input("enter score num 3"))
    number3.append(c)
    
    d = int(input("enter score num 4"))
    number4.append(d)
    
    e = int(input("enter score num 5"))
    number5.append(e)
        
    count += 1
    
import pandas as pd    

df = pd.DataFrame(data={
    'names': ['d', 's', 'r', 'l', 'a'],
    'match1': [number1, number2, number3, number4, number5],
}) 

df["Rank"] = df["match1"].rank()
df["Rank"] = df["match1"].rank(method ='max')
df

code in question

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 :

Assuming your dataframe is built correctly, you need to add ascending=False to your rank() method for the highest value to be rank 1:

df["rank"] = df["match1"].rank(ascending=False)

learn more with the method documentation here

EDIT: Data type issue. The data in match1 column are lists, which are ‘object’ datatypes in Pandas, which rank() cannot see/evaluate. Try this:

while count < 1:
    a = int(input("enter score num 1"))
    b = int(input("enter score num 2"))
    c = int(input("enter score num 3"))
    d = int(input("enter score num 4"))
    e = int(input("enter score num 5"))
    count += 1

import pandas as pd

df = pd.DataFrame(
    data={
        "names": ["d", "s", "r", "l", "a"],
        "match1": [a, b, c, d, e],
    }
)

df["rank"] = df["match1"].rank(ascending=False)
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