df.sort_values()
df.sort_values(by=column_name, ascending=True)
Sorts the entire DataFrame in order by the values in column_name
. ascending
can be omitted, as its default value is True.
- Input:
- column_name : string
- Column name to sort by.
- ascending : boolean, default 'True'
- Sort ascending or descending.
- Returns:
- A new DataFrame with the specified column sorted in ascending/descending.
- Return Type:
- DataFrame
pets = pets.sort_values(by='Weight', ascending=True)
pets
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! |
1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
7 | cat_003 | cat | black | 10 | 0 | True | No, thank you! |
2 | cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
pets = pets.sort_values(by='Age', ascending=False)
pets
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment |
---|---|---|---|---|---|---|---|
2 | cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** |
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. |
5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. |
1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! |
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! |
7 | cat_003 | cat | black | 10 | 0 | True | No, thank you! |