Home > database >  Dataframe column doesn't sort
Dataframe column doesn't sort

Time:02-01

Trying to sort the dataframe to show the highest value rows first but no matter what I try, nothing changes

new = pandas.DataFrame(d.items(), columns=['Trait', 'Count'])
new.sort_values('Count', ascending=False)
new['Percent'] = (new.Count / 1000) * 100
print(new)

What it's showing:

'Count' should be sorted in descending order by highest value to lowest and then obviously the equivalent row data should follow

                                Trait  Count  Percent
0                               Black    835     83.5
1                        Actual White     31      3.1
2                           Paperback     60      6.0
3                        Grape Poison      8      0.8
4                               Cielo      3      0.3
5                                Acid      6      0.6
6                               Olive      1      0.1
7                              Mailer     37      3.7
8                                Domo      7      0.7
9                               Hazel      4      0.4
10  Pink that is Almost Orange in Hue      2      0.2
11                              Vapor      1      0.1
12                               Grey      3      0.3
13                    MailerConstruct      1      0.1
14                              Blood      1      0.1

CodePudding user response:

you forget to add to new data frame try this code instead

new = pandas.DataFrame(d.items(), columns=['Trait', 'Count'])
new = new.sort_values('Count', ascending=False)
new['Percent'] = (new.Count / 1000) * 100
print(new)

CodePudding user response:

Try this at line 2:

new = new.sort_values('Count', ascending=False)

or

new.sort_values('Count', ascending=False, inplace=True)

CodePudding user response:

You have to set the values to the variable or set inplace=True

new.sort_values('Count', ascending=False, inplace=True)

or

new = new.sort_values('Count', ascending=False)
  • Related