30 Dec Sorting in Pandas
To sort the data in Pandas, we can use various methods. In this lesson, we will see how to sort the DataFrame in Pandas using the sort_values() method. Let us first see some examples:
- Sort the Pandas DataFrame (default ascending order)
- Sort the Pandas DataFrame in Descending Order
Sort the Pandas DataFrame (default ascending order)
To sort the dataframe, use the sort_values() method. The default is ascending. Therefore, you don’t need to mention any value in the parameter for ascending sort.
We will sort by value i.e. the by parameter will be set to the column name by which we need to sort the DataFrame in ascending order. Let us now see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import pandas as pd # Dataset data = { 'Student': ["Amit", "John", "Jacob", "David", "Steve"], 'Rank': [1, 4, 3, 5, 2], 'Marks': [95, 70, 80, 60, 90] } # Create a DataFrame using the DataFrame() method with index res = pd.DataFrame(data, index=['RowA', 'RowB', 'RowC', 'RowD', 'RowE'], ) # Display the Records print("Student Records\n\n", res) # Sort the data in ascending order (default) # The data is sort by the 'Rank' column using the 'by' parameter print("\nSorting in ascending order:\n", res.sort_values(by=['Rank'])) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Student Records Student Rank Marks RowA Amit 1 95 RowB John 4 70 RowC Jacob 3 80 RowD David 5 60 RowE Steve 2 90 Sorting in ascending order: Student Rank Marks RowA Amit 1 95 RowE Steve 2 90 RowC Jacob 3 80 RowB John 4 70 RowD David 5 60 |
Sort the Pandas DataFrame in Descending Order
To sort the dataframe in descending order, use the sort_values() method. Set the ascending parameter of the method to False for descending order sort.
We will sort by value i.e. the by parameter will be set to the column name by which we need to sort the DataFrame in descending order. Let us now see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import pandas as pd # Dataset data = { 'Student': ["Amit", "John", "Jacob", "David", "Steve"], 'Rank': [1, 4, 3, 5, 2], 'Marks': [95, 70, 80, 60, 90] } # Create a DataFrame using the DataFrame() method with index res = pd.DataFrame(data, index=['RowA', 'RowB', 'RowC', 'RowD', 'RowE'], ) # Display the Records print("Student Records\n\n", res) # Sort the data in descending order using the 'ascending' parameter with 'False' value # The data is sort by the 'Rank' column using the 'by' parameter print("\nSorting in descending order:\n", res.sort_values(by=['Rank'], ascending=False)) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Student Records Student Rank Marks RowA Amit 1 95 RowB John 4 70 RowC Jacob 3 80 RowD David 5 60 RowE Steve 2 90 Sorting in descending order: Student Rank Marks RowD David 5 60 RowB John 4 70 RowC Jacob 3 80 RowE Steve 2 90 RowA Amit 1 95 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments