Skip to main content

df.sort_values()

df.sort_values(by=column_name, ascending=True)

Sorts the entire DataFrame in ascending order by the values in the column. ascending can be omitted, as it's default value is True.

Input:
column_name : string
Column name to sort by.
ascending : boolean, default 'True'
Sort ascending vs. descending.
Returns:
df_sorted - A new DataFrame with the specified column sorted in ascending/descending.
Return Type:
DataFrame

pets = pets.sort_values(by='Weight', ascending=True)
pets
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
6ham_002hamstergolden0.250.2FalseNo, thank you!
5ham_001hamsterblack13FalseNo, thank you!
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
7cat_003catblack100TrueNo, thank you!
2cat_002catblack159True****All you need is love and a cat.****
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
0dog_001dogblack405False There are no bad dogs, only bad owners.
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
pets = pets.sort_values(by='Age', ascending=False)
pets
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
2cat_002catblack159True****All you need is love and a cat.****
0dog_001dogblack405False There are no bad dogs, only bad owners.
5ham_001hamsterblack13FalseNo, thank you!
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!