Home > Software engineering >  Return elements of a column based on a different column in Pandas
Return elements of a column based on a different column in Pandas

Time:01-25

I want to write a program to return names in a list based on the number of reports in descending order. like ['Jack', 'Joe', 'Rick'....]

df=
Number_of_reports  Name
5                   Rick
4                   Amanda
7                   Joe
8                   Jack
2                   Ryan
mylist=[]
greater_value=0
for i in df['Number_of_Reports']:
    if greater_value > i:
        mylist.append(df['Name'])

Any help will be greatly appreciated

CodePudding user response:

You can use sort_values and to_list:

names = df.sort_values(by='Number_of_reports', ascending=False)['Name'].tolist()

Which gives:

['Jack', 'Joe', 'Rick', 'Amanda', 'Ryan']

  •  Tags:  
  • Related