Python_newbie
Python_newbie

Reputation: 111

Return elements of a column based on a different column in Pandas

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

Upvotes: 0

Views: 91

Answers (1)

sjw
sjw

Reputation: 6543

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

Upvotes: 3

Related Questions