orange2piece
orange2piece

Reputation: 39

Dataframe column doesn't sort

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

Upvotes: 0

Views: 37

Answers (3)

Ashok KS
Ashok KS

Reputation: 691

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)

Upvotes: 0

Ali Redha
Ali Redha

Reputation: 335

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)

Upvotes: 1

Anastasiya-Romanova 秀
Anastasiya-Romanova 秀

Reputation: 3368

Try this at line 2:

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

or

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

Upvotes: 0

Related Questions