(Tutorial) Pandas Sort Values – DataCamp
[ad_1]
Discovering attention-grabbing bits of information in a DataFrame is usually simpler in the event you change the rows’ order. You’ll be able to type the rows by passing a column title to .sort_values()
.
In circumstances the place rows have the identical worth (that is frequent in the event you type on a categorical variable), you might want to break the ties by sorting on one other column. You’ll be able to type on a number of columns on this manner by passing a listing of column names.

Modifying the Order of Columns
You’ll be able to change the rows’ order by sorting them in order that essentially the most attention-grabbing information is on the prime of the dataframe.
For instance, once we apply sort_values()
on the weight_kg
column of the canine dataframe, we get the lightest canine on the prime, Stella the Chihuahua, and the heaviest canine on the backside, Bernie the Saint Bernard.
canine.sort_values("weight_kg")
title breed shade height_cm weight_kg date_of_birth
5 Stella Chihuahua Tan 18 2 2015-04-20
3 Cooper Schnauzer Grey 49 17 2011-12-11
0 Bella Labrador Brown 56 24 2013-07-01
1 Charlie Poodle Black 43 24 2016-09-16
2 Lucy Chow Chow Brown 46 24 2014-08-25
4 Max Labrador Black 59 29 2017-01-20
6 Bernie St. Bernard White 77 74 2018-02-27
Setting the ascending
argument to False will type the information the opposite manner spherical, from heaviest to lightest canine.
canine.sort_values("weight_kg", ascending=False)
title breed shade height_cm weight_kg date_of_birth
6 Bernie St. Bernard White 77 74 2018-02-27
4 Max Labrador Black 59 29 2017-01-20
0 Bella Labrador Brown 56 24 2013-07-01
1 Charlie Poodle Black 43 24 2016-09-16
2 Lucy Chow Chow Brown 46 24 2014-08-25
3 Cooper Schnauzer Grey 49 17 2011-12-11
5 Stella Chihuahua Tan 18 2 2015-04-20
Sorting by A number of Variables
We are able to type by a number of variables by passing a listing of column names to sort_values
. Right here, we type first by weight, then by peak. Now, Charlie, Lucy, and Bella are ordered from shortest to tallest, although all of them weigh the identical.
canine.sort_values(["weight_kg", "height_cm"])
title breed shade height_cm weight_kg date_of_birth
5 Stella Chihuahua Tan 18 2 2015-04-20
3 Cooper Schnauzer Grey 49 17 2011-12-11
1 Charlie Poodle Black 43 24 2016-09-16
2 Lucy Chow Chow Brown 46 24 2014-08-25
0 Bella Labrador Brown 56 24 2013-07-01
4 Max Labrador Black 59 29 2017-01-20
6 Bernie St. Bernard White 77 74 2018-02-27
To alter the course values are sorted in, move a listing to the ascending argument to specify which course sorting needs to be accomplished for every variable. Now, Charlie, Lucy, and Bella are ordered from tallest to shortest.
canine.sort_values(["weight_kg", "height_cm"], ascending=[True, False])
title breed shade height_cm weight_kg date_of_birth
5 Stella Chihuahua Tan 18 2 2015-04-20
3 Cooper Schnauzer Grey 49 17 2011-12-11
0 Bella Labrador Brown 56 24 2013-07-01
2 Lucy Chow Chow Brown 46 24 2014-08-25
1 Charlie Poodle Black 43 24 2016-09-16
4 Max Labrador Black 59 29 2017-01-20
6 Bernie St. Bernard White 77 74 2018-02-27
Interactive Instance
Within the following instance, you’ll type homelessness
by the variety of homeless people, from smallest to largest, and save this as homelessness_ind
. Lastly, you’ll print the pinnacle of the sorted DataFrame.
# Kind homelessness by people
homelessness_ind = homelessness.sort_values("people")
# Print the highest few rows
print(homelessness_ind.head())
Once we run the above code, it produces the next outcome:
area state people family_members state_pop
50 Mountain Wyoming 434.0 205.0 577601
34 West North Central North Dakota 467.0 75.0 758080
7 South Atlantic Delaware 708.0 374.0 965479
39 New England Rhode Island 747.0 354.0 1058287
45 New England Vermont 780.0 511.0 624358
To study extra about sorting and subsetting the information, please see this video from our course Data Manipulation with pandas.
This content material is taken from DataCamp’s Data Manipulation with pandas course by Maggie Matsui and Richie Cotton.
[ad_2]
Source link